if !ENV['RUBY_THREAD_VM_STACK_SIZE']
  exec({'RUBY_THREAD_VM_STACK_SIZE'=>'1000000000'}, '/usr/bin/ruby', $0)
else
  n, m = gets.split.map(&:to_i)
  @edge = []
  @maxes = []
  def dp(x)
    return @maxes[x] if @maxes[x]
    @maxes[x] = @edge[x]&.map{ |v| dp(v) + 1 }&.max || 0
  end

  m.times do
    u, v = gets.split.map(&:to_i)
    if @edge[u].nil?
      @edge[u] = [v]
    else
      @edge[u] << v
    end
  end
  ans = 0
  (1..n).each { |u| ans = dp(u) if dp(u) > ans }
  puts ans
end
