import sys

sys.setrecursionlimit(10**9)

 

 

N, M = list(map(int, input().split()))

 

G = [[] for _ in range(N + 1)]

for i in range(M):

    x, y = list(map(int, input().split()))

    G[x] += [y]

# print(G)

 

dp = [-1] * (N + 1)

 

 

def func(n):

    if dp[n] != -1:

        return dp[n]

    cnt = 0

	# 先にノードが存在する場合

	# ノードが存在しない場合は、cnt=0のままforをすっ飛ばす

    for m in G[n]:

        cnt = max(cnt, func(m) + 1)

    dp[n] = cnt

    return cnt

 

 

for i in range(1, N + 1):

    func(i)

 

# print(dp)

print((max(dp)))
#G

import sys

sys.setrecursionlimit(10**9)



N,M = list(map(int, input().split()))

x = [0] * M

y = [0] * M

edge=[[] for j in range(N+1)]

for i in range(M):

    x[i], y[i] = list(map(int, input().split()))

    edge[x[i]]+=[y[i]]



dp=[0]*(N+1)



def f(x):

    if dp[x]!=0:

        return dp[x]

    num=0

    for i in edge[x]:

        if i==[]:

            num=max(num,0)

        else:

            num=max(num,f(i)+1)

    dp[x]=num

    return dp[x]



for i in range(1,N+1):

    f(i)

print((max(dp)))
import sys

sys.setrecursionlimit(10**9)
N, M = list(map(int, input().split()))
G = [[] for _ in range(N + 1)]
for i in range(M):
    x, y = list(map(int, input().split()))
    G[x] += [y]
# print(G)
dp = [-1] * (N + 1)


def func(n):
    if dp[n] != -1:
        return dp[n]
    cnt = 0
    # 先にノードが存在する場合
    # ノードが存在しない場合は、cnt=0のままforをすっ飛ばす
    for m in G[n]:
        cnt = max(cnt, func(m) + 1)
    dp[n] = cnt
    return cnt


for i in range(1, N + 1):
    func(i)
# print(dp)
print((max(dp)))

# G
import sys

sys.setrecursionlimit(10**9)
N, M = list(map(int, input().split()))
x = [0] * M
y = [0] * M
edge = [[] for j in range(N + 1)]
for i in range(M):
    x[i], y[i] = list(map(int, input().split()))
    edge[x[i]] += [y[i]]
dp = [0] * (N + 1)


def f(x):
    if dp[x] != 0:
        return dp[x]
    num = 0
    for i in edge[x]:
        if i == []:
            num = max(num, 0)
        else:
            num = max(num, f(i) + 1)
    dp[x] = num
    return dp[x]


for i in range(1, N + 1):
    f(i)
print((max(dp)))

+# G -G = [[] for _ in range(N + 1)] +x = [0] * M +y = [0] * M +edge = [[] for j in range(N + 1)] -    x, y = list(map(int, input().split())) -    G[x] += [y] -# print(G) -dp = [-1] * (N + 1) +    x[i], y[i] = list(map(int, input().split())) +    edge[x[i]] += [y[i]] +dp = [0] * (N + 1) -def func(n): -    if dp[n] != -1: -        return dp[n] -    cnt = 0 -    # 先にノードが存在する場合 -    # ノードが存在しない場合は、cnt=0のままforをすっ飛ばす -    for m in G[n]: -        cnt = max(cnt, func(m) + 1) -    dp[n] = cnt -    return cnt +def f(x): +    if dp[x] != 0: +        return dp[x] +    num = 0 +    for i in edge[x]: +        if i == []: +            num = max(num, 0) +        else: +            num = max(num, f(i) + 1) +    dp[x] = num +    return dp[x] -    func(i) -# print(dp) +    f(i)