Problem:
Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:

Solution in Lua (5.3.2) (judge verdict: Accepted)

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)