N, M = gets.split.map(&:to_i)
@nodes = Array.new(N + 1) { [] }
M.times.map do
  x, y = gets.split.map(&:to_i)
  @nodes[x] ||= []
  @nodes[x] << y
end

@dp = []

def solve(i)
  return @dp[i] if @dp[i]

  max = 0
  @nodes[i].each do |to|
    solved = solve(to) + 1
    max = solved if solved > max
  end

  @dp[i] = max
  return max
end

ans = 0
1.upto(N) do |i|
  solved = solve(i)
  ans = solved if ans < solved
end

puts ans
