program check;

uses
	testlib, sysutils;

const
	maxn = 100;

var
	ja, pa, pc: longint;
	i, j, k, n: longint;
	prev: longint;
	u: array [1..maxn, 1..maxn] of boolean;
	b, r: array [1..maxn] of longint;
	w: array [1..maxn] of boolean;

begin
    ja := ans.readlongint;
    pa := ouf.readlongint;

    if pa > ja then
    	quit(_wa, format('expected: %d, found: %d', [ja, pa]));
	
	n := inf.readlongint;
	for i := 1 to n do begin
		b[i] := inf.readlongint;
		r[i] := inf.readlongint;
		for j := 1 to r[i] do begin
			k := inf.readlongint;
			u[i][k] := true;
		end;
	end;

	pc := 0;
	for i := 1 to n do begin
		j := ouf.readlongint;
		if (j < 1) or (j > n) then
			quit(_wa, format('invalid step number: %d', [j]));
		if w[j] then
			quit(_wa, format('step repeated: %d', [j]));
		for k := 1 to n do
			if u[j][k] and not w[k] then
				quit(_wa, format('cannot perform step %d before step %d', [j, k]));
		w[j] := true;
		if i > 1 then
			if b[j] <> prev then
				inc(pc);
		prev := b[j];
	end;

	if pa <> pc then
		quit(_wa, format('promised: %d, calculated: %d', [pa, pc]));

	if pa < ja then
    	quit(_fail, format('expected: %d, found: %d', [ja, pa]));

    quit(_ok, format('%d moves', [pa]));
end.

