n,m = gets.chomp.split(" ").map(&:to_i)
pass = Array.new(m).map{Array.new(2, 0)}
for i in 0..m-1
  pass[i] = gets.chomp.split(" ").map(&:to_i)
end
dp = Array.new(n, 0)
for i in 0..m-1
  if dp[pass[i][1]-1] < dp[pass[i][0]-1]+1
    dp[pass[i][1]-1] = dp[pass[i][0]-1]+1
  end
end
dp.sort!
puts dp[-1]
