N, M = gets.split.map(&:to_i)

edgse = M.times.map { gets.split.map(&:to_i) }

dp = Array.new(N + 1) { Array.new(2, 0) }

edgse.each do |from, to|
  go = dp[from][0] + 1
  back = dp[to][1] + 1

  if dp[to][0] < go
    dp[to][0] = go
  end

  if dp[from][1] < back
    dp[from][1] = back
  end
end

puts (1..N).map { |i| dp[i][0] + dp[i][1] }.max
