require 'set'

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

MEMO = Array.new(N + 1, nil)

G = Array.new(N + 1) { [] }

M.times do
  x, y = gets.chomp.split.map(&:to_i)

  G[x] << y
end

def calc(i)
  return MEMO[i] if MEMO[i]

  return 0 if G[i].empty?

  MEMO[i] = G[i].map {|j| calc(j) }.max + 1
end

print (1..N).map {|i| calc(i) }.max || 0
