require 'tsort'

N, M = gets.split.map(&:to_i)
g = Hash.new
(1..N).each do |i|
  g[i] = []
end

M.times do
  from, to = gets.split.map(&:to_i)
  g[from] << to
end

order = []

begin
  each_node = -> (&b) { g.each_key(&b) }
  each_child = -> (n, &b) { g[n].each(&b) }
  order = TSort.tsort(each_node, each_child).reverse

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

  order.each do |from|
    g[from].each do |to|
      len = dp[from] + 1
      dp[to] = len if dp[to] < len
    end
  end
rescue
end

puts dp.max
