local n, m = io.read("*n", "*n")
local edge = {}
local cnt = {}
local len = {}
for i = 1, n do
  edge[i] = {}
  cnt[i] = 0
  len[i] = 0
end
for i = 1, m do
  local x, y = io.read("*n", "*n")
  table.insert(edge[x], y)
  cnt[y] = cnt[y] + 1
end
local tasks = {}
for i = 1, n do
  if cnt[i] == 0 then
    table.insert(tasks, i)
  end
end
local mma = math.max
while 0 < #tasks do
  local src = tasks[#tasks]
  table.remove(tasks)
  for i = 1, #edge[src] do
    local dst = edge[src][i]
    len[dst] = mma(len[dst], len[src] + 1)
    cnt[dst] = cnt[dst] - 1
    if cnt[dst] == 0 then
      table.insert(tasks, dst)
    end
  end
end
local ret = 0
for i = 1, n do
  ret = mma(ret, len[i])
end
print(ret)
