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
@has=Hash.new
for i in 1..N
  @has[i]=Array.new
end
for i in 1..M
  d=gets.split.map &:to_i
  @has[d[0]] << d[1]
end
@idx=@has.tsort
# p @has
# p @idx
@memo=Array.new(N+1,nil)

def dp(n)
  return @memo[n] if !@memo[n].nil?
  if @has[n].empty?
    @memo[n]=0
    return 0
  else
    output=(@has[n].map{|x|dp(x)}.max)+1
    @memo[n]=output
    return output
  end
end
max=0
for i in 1..N
  cnt=dp(@idx.index(i)+1)
  max = max < cnt ? cnt : max
end
puts max
