local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local unpack = table.unpack or unpack
local insert = table.insert or insert
local max = math.max
----
local function adj_list(num_nodes)
    local t = {}
    for n=1, num_nodes do
        t[n] = {}
    end
    return t
end
----
local N, M = read.nn()
local edges = adj_list(N)
local rev_adj_mat = array(2, nil)
for i=1,M do
    local x, y = read.nn()
    edges[x][#edges[x] + 1] = y
    rev_adj_mat[y][x] = true
end
local function indeg_zero(n)
    for k, v in pairs(rev_adj_mat[n]) do
        if v then
            return false
        end
    end
    return true
end

local toposorted = {}
local q = {}
for n=1,N do
    if indeg_zero(n) then
        insert(q, n)
    end
end
local front = 1
local back = #q + 1
local function push(v)
    q[back] = v
    back = back + 1
end
local function pop()
    local r = q[front]
    front = front + 1
    return r
end
local function empty()
    return back - front == 0
end

while not empty() do
    local n = pop()
    insert(toposorted, n)
    for k, m in pairs(edges[n]) do
        rev_adj_mat[m][n] = nil
        if indeg_zero(m) then
            push(m)
        end
    end
end
--[[
for k, v in pairs(toposorted) do
    print(k, v)
end
]]
local lp_from = array(1, 0)
for _, n in pairs(toposorted) do
    for _, m in pairs(edges[n]) do
        lp_from[m] = max(lp_from[m], lp_from[n] + 1)
    end
end
local ans = 0
for _, lp in pairs(lp_from) do
    ans = max(ans, lp)
end
print(ans)