def tsort(key,inCnt,out)
	s = []
	ret = []
	key.each do |v|
		s << v if inCnt[v] == 0
	end
	while v = s.shift
		ret << v
		out[v].each do |u|
			inCnt[u] -= 1
			s << u if inCnt[u] == 0
		end
	end
	return ret
end

N,M = gets.split.map(&:to_i)
dp = [0]*(N+1)
inCnt= [0]*(N+1)
vin = (N+1).times.map{[]}
vout = (N+1).times.map{[]}

M.times do
	x,y = gets.split.map(&:to_i)
	inCnt[y] += 1
	vin[y] << x
	vout[x] << y
end

list = tsort((1..N),inCnt,vout)

list.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