/* Copyright (C) 2009 Igor V. Burago */

#include <vector>
#include "Check"

using namespace std;
using namespace Check;


const int MaxRounds = 1000;


int main(int argc, char *argv[])
{
    init(argc, argv);

    int runners_num;
    int pairs_num;
    input >> runners_num >> pairs_num;

    vector<int> antipode(runners_num, -1);
    for (int pos = 0; pos < pairs_num; ++pos)
    {
        int first;
        int second;
        input >> first >> second;
        --first;
        --second;
        antipode[first] = second;
        antipode[second] = first;
    }

    int rounds_num;
    output >> rounds_num;
    exitUnless(
        1 <= rounds_num && rounds_num <= MaxRounds,
        PresentationError, "Invalid rounds number: %d", rounds_num);

    vector<int> freq(runners_num, 0);
    vector< vector<int> > pairs_freq(runners_num);
    for (int runner = 0; runner < runners_num; ++runner)
        pairs_freq[runner].resize(runners_num, 0);
    vector< vector<int> > runner_tracks(runners_num);
    for (int runner = 0; runner < runners_num; ++runner)
        runner_tracks[runner].resize(runners_num - 1, 0);

    vector<int> runners(runners_num - 1);
    for (int round = 0; round < rounds_num; ++round)
    {
        for (int track = 0; track < runners_num - 1; ++track)
        {
            int num;
            output >> num;
            exitUnless(
                1 <= num && num <= runners_num,
                PresentationError, "Invalid player number: %d", num);
            runners[track] = --num;
            ++freq[num];
            ++runner_tracks[num][track];
        }
        for (int first = 0; first < runners_num - 1; ++first)
            for (int second = first + 1; second < runners_num - 1; ++second)
            {
                int one = runners[first];
                int another = runners[second];
                ++pairs_freq[one][another];
                ++pairs_freq[another][one];
            }
    }

    for (int runner = 1; runner < runners_num; ++runner)
        exitUnless(
            freq[runner - 1] == freq[runner],
            WrongAnswer, "Players participate different number of races");
    for (int runner = 0; runner < runners_num; ++runner)
    {
        exitUnless(
            pairs_freq[runner][runner] == 0,
            WrongAnswer, "Player %d occupies more than one track in a race", runner + 1);

        int count = runner_tracks[runner][0];
        for (int track = 1; track < runners_num - 1; ++track)
            exitUnless(
                runner_tracks[runner][track] == count,
                WrongAnswer, "Player %d runs different number of tracks");

        count = pairs_freq[runner][runner == 0];
        for (int other = 0; other < runners_num; ++other)
        {
            if (other == runner)
                continue;
            exitUnless(
                pairs_freq[runner][other] == count,
                WrongAnswer, "Player %d does not compete with others the same number of times",
                runner + 1);
        }
    }

    quit(SolutionAccepted);
}
