program leve2013;
var
ans,i,j,k,l,m,n,t,r:longint;
w:array[0..1000,0..1000] of boolean;
s,a,q:array[0..1000] of longint;
finish:boolean;
begin
assign(input,'level2013.in'); reset(input);
assign(output,'level2013.out'); rewrite(output);
readln(n,m);
for i:=1 to m do
begin
read(t);
for j:=1 to t do read(s[j]);
for j:=1 to t do
begin
if j<>1 then
for k:=s[j-1]+1 to s[j]-1 do
for l:=1 to t do
if not w[k,s[l]] then
begin
w[k,s[l]]:=true;
inc(a[s[l]]);
end;
end;
readln;
end;
while not finish do
begin
finish:=true;
inc(ans);
fillchar(q,sizeof(q),0);
r:=0;
for i:=1 to n do
if a[i]=0 then
begin
a[i]:=-1; q[r]:=i; inc(r);
end;
for i:=0 to r-1 do
for j:=1 to n do
if w[q[i],j] then
begin
finish:=false;
dec(a[j]);
end;
end;
writeln(ans);
close(input); close(output);
end.