local Deque={}

Deque.lpush=function(self,value)
    local first=self.first-1
    self.first=first
    self[first]=value
end

Deque.rpush=function(self,value)
    local last=self.last+1
    self.last=last
    self[last]=value
end

Deque.lpop=function(self)
    local first=self.first
    local value=self[first]
    self[first]=nil
    self.first=first+1
    return value
end

Deque.rpop=function(self)
    local last=self.last
    local value=self[last]
    self[last]=nil
    self.last=last-1
    return value
end

Deque.size=function(self)
    return self.last-self.first+1
end

Deque.empty=function(self)
    return self.first>self.last
end

Deque.new=function()
    local obj={first=0,last=-1}
    setmetatable(obj,{__index=Deque})
    return obj
end

----------

local n,m=io.read("n","n")
local g={}
local h={}
local dp={}
for i=1,n do
    g[i]={}
    h[i]=0
    dp[i]=0
end
for i=1,m do
    local x,y=io.read("n","n")
    table.insert(g[x],y)
    h[y]=h[y]+1
end

local que=Deque:new()
for x=1,n do
    if h[x]==0 then
        que:rpush(x)
    end
end
while not que:empty() do
    local x=que:lpop()
    for i=1,#g[x] do
        local y=g[x][i]
        h[y]=h[y]-1
        if h[y]==0 then
            que:rpush(y)
            dp[y]=math.max(dp[y],dp[x]+1)
        end
    end
end
local length=0
for i=1,n do
    length=math.max(length,dp[i])
end
print(length)