n, m = gets.split.map(&:to_i)

@edge = Array.new(n+1) {[]}
m.times do
    x, y = gets.split.map(&:to_i)
    @edge[x] << y
end

@memo = Array.new(n+1)
@memo[0] = 0

def calc(i)
    return @memo[i] if @memo[i]

    m = 0
    @edge[i].each do |e|
        x = calc(e) + 1
        m = x if m < x
    end

    @memo[i] = m
end

1.upto(n) {|i| calc(i)}
p @memo.max