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 N, M = read.nn()
local adj_mat = array(2, nil)
local rev_adj_mat = array(2, nil)
for i=1,M do
    local x, y = read.nn()
    adj_mat[x][y] = true
    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 function make_queue()
    local q = {}
    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
    return push, pop, empty
end
local push, pop, empty = make_queue()
for n=1,N do
    if indeg_zero(n) then
        push(n)
    end
end

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