N,M = gets.split.map &:to_i
es = Hash[(1..N).map{ |x| [x,{}]}]
M.times {
  x,y = gets.split.map &:to_i
  es[x][y] = 1
}
dp = Array.new(N,0)

(1..N).each{ |x|
  dig_stack = [x]
  climb_stack = []
  while k = dig_stack.pop do
    ns = es[k].keys
    if ns.size == 0
      dp[k-1] = 0
    else
      climb_stack.push(k)
      dig_stack.concat(ns)
    end
  end
  while k = climb_stack.pop do
    dp[k-1] = es[k].keys.map{ |y| dp[y-1] }.max + 1
  end
}

p dp.max
