import sys

sys.setrecursionlimit(500000)

n, m = list(map(int, input().split()))

G = [[] for _ in range(n)]

for _ in range(m):

    x, y = [int(x)-1 for x in input().split()]

    G[x].append(y)



# iを始点としたときの有効パスの最大長さ

dp = [-1] * n

 

def rec(s):

    if dp[s] != -1:

        return dp[s]



    ret = 0

    for ns in G[s]:

        ret = max(ret, rec(ns)+1)

    dp[s] = ret

    return ret



ml = 0    

for i in range(n):

    ml = max(ml, rec(i))

print(ml)
n, m = list(map(int, input().split()))

G = [[] for _ in range(n)]

deg = [0] * n # 各頂点の入力次数

for _ in range(m):

    x, y = [int(x)-1 for x in input().split()]

    G[x].append(y)

    deg[y] += 1



from collections import deque

# 入力次数が0の頂点をBFSの開始点とする

q = deque([])

for i in range(n):

    if deg[i] == 0:

        q.append(i)

dp = [0] * n



while q:

    v = q.popleft()



    for nv in G[v]:

        # vからnvへの入力を削除

        deg[nv] -= 1

        if deg[nv] == 0:

            q.append(nv)

            dp[nv] = max(dp[nv], dp[v]+1)

    

print((max(dp)))

import sys

sys.setrecursionlimit(500000)
n, m = list(map(int, input().split()))
G = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) - 1 for x in input().split()]
    G[x].append(y)
# iを始点としたときの有効パスの最大長さ
dp = [-1] * n


def rec(s):
    if dp[s] != -1:
        return dp[s]
    ret = 0
    for ns in G[s]:
        ret = max(ret, rec(ns) + 1)
    dp[s] = ret
    return ret


ml = 0
for i in range(n):
    ml = max(ml, rec(i))
print(ml)

n, m = list(map(int, input().split()))
G = [[] for _ in range(n)]
deg = [0] * n  # 各頂点の入力次数
for _ in range(m):
    x, y = [int(x) - 1 for x in input().split()]
    G[x].append(y)
    deg[y] += 1
from collections import deque

# 入力次数が0の頂点をBFSの開始点とする
q = deque([])
for i in range(n):
    if deg[i] == 0:
        q.append(i)
dp = [0] * n
while q:
    v = q.popleft()
    for nv in G[v]:
        # vからnvへの入力を削除
        deg[nv] -= 1
        if deg[nv] == 0:
            q.append(nv)
            dp[nv] = max(dp[nv], dp[v] + 1)
print((max(dp)))

-import sys - -sys.setrecursionlimit(500000) +deg = [0] * n  # 各頂点の入力次数 -# iを始点としたときの有効パスの最大長さ -dp = [-1] * n +    deg[y] += 1 +from collections import deque - -def rec(s): -    if dp[s] != -1: -        return dp[s] -    ret = 0 -    for ns in G[s]: -        ret = max(ret, rec(ns) + 1) -    dp[s] = ret -    return ret - - -ml = 0 +# 入力次数が0の頂点をBFSの開始点とする +q = deque([]) -    ml = max(ml, rec(i)) -print(ml) +    if deg[i] == 0: +        q.append(i) +dp = [0] * n +while q: +    v = q.popleft() +    for nv in G[v]: +        # vからnvへの入力を削除 +        deg[nv] -= 1 +        if deg[nv] == 0: +            q.append(nv) +            dp[nv] = max(dp[nv], dp[v] + 1) +print((max(dp)))