N, M = gets.split.map(&:to_i)
lines = {}
M.times do
  s, t = gets.split.map(&:to_i)
  (lines[s-1] ||= []) << t-1
end
# p lines
dp = Array.new(N, 0)
# binding.pry
loop do
  check = false
  N.times do |from|
    if lines[from]
      lines[from].each do |to|
        # binding.pry
        if dp[to] < dp[from] + 1
          dp[to] = dp[from] + 1
          check = true
        end
      end
    end
  end
  break if !check
end
puts dp.max