integer N,M
integer,allocatable,dimension(:,:)::Route
integer X,Y
read*,N,M
allocate(Route(N,N))
Route=-huge(Route)/4
do i=1,N
  Route(i,i)=0
end do
do i=1,M
  read*,x,y
  Route(x,y)=1
end do

call Warshall_Floyd(n,Route)

print"(I0)",maxval(Route)

contains

subroutine Warshall_Floyd(n,Route)
  !nは頂点の数、Routeは全ての経路の長さを記録したもの
  !以下の型宣言は場合に応じて変えること
  implicit none
  integer, intent(in) :: n
  integer, intent(inout) :: Route(n, n)
  integer :: i, j, k

    do k = 1, n
      do i = 1, n
        do j = 1, n
          Route(j, i) = max( Route(j, i), Route(j, k) + Route(k, i))
        end do
      end do
    end do
end subroutine Warshall_Floyd
end