def max(a, b)
  a > b ? a : b
end

def dfs(i)
  return if @used[i]
  @used[i] = true
  @graph[i].each do |n|
    dfs(n)
  end
  @v << i
end

def tsort()
  N.times do |i|
    dfs(i)
  end
  @v.reverse
end

N, M = gets.split.map(&:to_i)
XY = M.times.map{gets.split.map(&:to_i)}

@used = Array.new(N, false)
@v = []
@graph = Array.new(N){[]}
M.times do |i|
  x, y = XY[i]
  @graph[x - 1] << y - 1
end

topo = tsort()
dp = Array.new(N, 0)
topo.each do |i|
  @graph[i].each do |j|
    dp[j] = max(dp[j], dp[i] + 1)
  end
end

puts dp.max