module queue
  implicit none
  type :: JagArray
    integer,allocatable :: array(:)
  end type JagArray
  contains
  subroutine push(A,b)
  	integer,allocatable,dimension(:)::A
    integer::b
    A=[A,b]
  end subroutine
  subroutine pop(A)
  	integer,allocatable,dimension(:)::A
    A=A(2:)
  end subroutine
end module queue

use queue
integer N,M
integer X,Y
type(JagArray),allocatable,dimension(:)::EDGE
integer,allocatable,dimension(:)::DP
integer ans

read*,N,M
allocate(EDGE(N),DP(N))
do i=1,N
  EDGE(i)%array=[integer::]
end do
do i=1,M
  read*,X,Y
  call push(EDGE(X)%array,Y)
end do

DP = -1
ans = 0
do i=1,N
  if( DP(i)== -1)DP(i)=DFS(i)
  ans=max(ans,DP(i) )
end do

print"(I0)",ans

contains

recursive function DFS(u)result(res)
integer u,res

if(DP(u) >= 0)then
  res=DP(u)
else
  DP(u)=0
  do while(size(EDGE(u)%array)/=0)
    DP(u)=max(DP(u),DFS(EDGE(u)%array(1))+1)
    call pop(EDGE(u)%array)
  end do
  res=DP(u)
endif
end function DFS
end