{
TASK : CARS
LANG : PASCAL
}
{$R-}
const maxn = 987654321;
type data = record
     vr,next:longint;
   end;
Var a:array[1..4001000] of data;
    start:array[1..500100] of longint;
    ind1,n,i,x:longint;
    Fmem,p:longint;
    com:array[1..500100] of longint;
    j:longint;
    d:array[1..500100] of longint;
    c,c1:array[1..500100] of boolean;
    op:array[1..500100] of longint;
Procedure BFS;
var first,last:longint;
begin
   last:=1;
   first:=0;
   c[ind1]:=true;
   op[1]:=ind1;
   while first<last do
      begin
         inc(first);
         p:=start[op[first]];
         while a[p].next<>-1 do
            begin
               if c[a[p].vr]=false then
                  begin
                     c[a[p].vr]:=true;
                     inc(last);
                     op[last]:=a[p].vr;
                  end;
               d[a[p].vr]:=((d[op[first]] mod maxn)+1) mod maxn;
               p:=a[p].next;
            end;
      end;
   p:=0;
   for i:=1 to n do if c1[i]=false then p:=d[i];
   writeln(p);
end;
Procedure Readall;
Begin
   readln(n);
   Fmem:=0;
   for i:=1 to n do
      begin
         read(p);
         com[i]:=p;
         if p = 0 then ind1:= i;
         for j:=1 to p do
            begin
               read(x);
               if start[x]=0 then
                  begin
                     inc(fmem);
                     a[Fmem].vr:=0;
                     a[Fmem].next:=-1;
                     start[x]:=Fmem;
                  end;
               if start[i]=0 then
                  begin
                     inc(Fmem);
                     a[Fmem].vr:=0;
                     a[Fmem].next:=-1;
                     start[i]:=Fmem;
                  end;
               inc(Fmem);
               c1[x]:=true;
               a[Fmem].vr:=i;
               a[Fmem].next:=start[x];
               start[x]:=Fmem;
            end;
         readln;
      end;
End;
Begin
   Readall;
   BFS;
End.
