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

(1..N).each{ |x|
  dig_stack = [x]
  climb_stack = []
  #p x
  xs = {}
  while k = dig_stack.pop do
    next if dp[k-1] > 0 || xs[k]
    ns = es[k]
    xs[k] = 1
    if ns.size > 0
      climb_stack.push(k)
      dig_stack.concat(ns)
    end
    #p [k,dig_stack,climb_stack]
  end
  climb_stack.reverse_each{ |k|
    v = 0
    es[k].each{ |y| v = dp[y-1] > v ? dp[y-1] : v }
    dp[k-1] = v + 1
  }
}

p dp.max
