var x,y,z,k,i,j,n,m,v,l:longint;
a:array[1..1000,0..1000] of longint;
b:array[1..1000] of longint;
 begin
 
  read(n,m);
  for i:=1 to m do
   begin
    read(x,y);
    a[x,y]:=1;
    if y>a[x,0] then a[x,0]:=y;
    if b[x]=0 then k:=k+1;
    b[x]:=1;
   end;
  while k>0 do
   begin
    x:=1;
    for i:=1 to n do
    begin
     if (b[i]=1) and (a[i,0]>=x) then
      begin
       if x>1 then
       begin
       l:=x;
       x:=a[i,0];
        for j:=l-1 downto 1 do
       if a[i,j]=1 then begin a[i,0]:=j; break;end;
       if (j=1) and (a[i,j]=0) then begin b[i]:=0; k:=k-1; end;
       end
       else begin b[i]:=0; k:=k-1; x:=a[i,0];end;
      end;
     end;
    z:=z+1;
   end;
  writeln(z);
 
 end.
