var
 a:array[1..5000,1..5000]of integer;
 i,j,k,l,m,ukn,ukp,n,p,otvet,g,v:longint;
 ffprod,f,ffnah:boolean;
 ffff:text;
procedure find(gor,vert:longint;var ff:boolean;var num:longint);
 var
  x,y:longint;
  ffin:boolean;
 begin
  y:=vert;
  ffin:=false;
  for x:=vert to ukn do
   begin
    if a[gor,x]=1 then
     begin
      y:=x;
      a[gor,x]:=0;
      ffin:=true;
     end;
   end;
  ff:=ffin;
  num:=y;
 end;
begin
 read(ukn,ukp);
 fillchar(a,sizeof(a),0);
 for i:=1 to ukp do
  begin
   read(m,n);
   a[m,n]:=1;
  end;
 {close(ffff);
 assign(ffff,'output.txt');
 rewrite(ffff);}
 ffprod:=true;
 otvet:=0;
 while ffprod do
  begin
   v:=1;
   f:=false;
   for g:=1 to ukn do
    begin
     find(g,v,ffnah,v);
     if ffnah=true then
      begin
       f:=true;
      end;
    end;
   if f then
    inc(otvet)
        else
    ffprod:=false;
  end;
 write(otvet);
 {close(ffff);}
end.
