uses crt;
var
  tin,tout:text;

  graph:array[1..1000,1..1000] of integer;
  node,visit:array[1..100] of integer;
  s:string;
  x,aa,cc,y,bb,n,m:longint;
  found:boolean;
  procedure cycle(z,w:integer);
  var
  aaa:integer;
  begin
    visit[z]:=1;
    if graph[z,bb] = 1 then
    begin
      writeln(tout,w+1);
      found:=true;
      exit;
    end;
    for aaa:= 1 to 100 do
    begin
      if graph[z,aaa] = 1 then
      if not found then
      begin
        cycle(aaa,w+1);
      end;
    end;
    visit[z]:=0;
  end;
begin
  assign(tin,'DATA5.txt');reset(tin);
  assign(tout,'OUT5.txt');rewrite(tout);
  for aa:= 1 to 5 do begin
      readln(tin,x);
      for bb:= 1 to 100 do
        for cc:= 1 to 100 do graph[bb,cc]:=0;
      for bb:= 1 to x do
      begin
        readln(tin,n,m);
        graph[n,m]:=1;
        node[n]:=1;
      end;
      found:=false;
      for bb:= 1 to 100 do
      begin
        if node[bb] = 1 then
        cycle(bb,0);
        if found = true then break;
      end;
  end;
  close(tout);
end.



