require 'tsort'

class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end
end

N,M = gets.split.map(&:to_i)
dp = [0]*(N+1)
vIn = Hash[N.times.map{|i| [i+1,[]]}]

M.times do
	x,y = gets.split.map(&:to_i)
	vIn[y] << x
end

vIn.tsort.each do |t|
	max = -1
	vIn[t].each do |j|
		max = dp[j] if max < dp[j]
	end
	dp[t] = max+1
	#p [t,vIn[t],dp[t]]
end
p dp.max

