n, m = gets.split.map(&:to_i)
edges = Array.new(m){gets.split.map(&:to_i)}

moves = Array.new(n+1){[]}
edges.each{|e| moves[e[0]] << e[1]}

d = Array.new(n+1, 0)
queue = []
starts = (1..n).to_a - edges.map(&:last)
starts.each do |s|
  queue << s
  until queue.empty?
    u = queue.pop
    moves[u].each do |v|
      if d[v] < d[u] + 1 
        d[v] = d[u] + 1
        queue.push(v)
      end
    end
  end
end
puts d.max
