import sys



sys.setrecursionlimit(10**9)



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



g = [[] for i in range(n+1)]



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

for _ in range(m):

    v1 , v2 = list(map(int,input().split()))

    g[v1].append(v2)

    

# finding longest path for vertice i

def dfs(i):

    if dp[i]!=-1:

        return dp[i]

    temp = 0

    for nv in g[i]:

        temp = max(temp, dfs(nv)+1)



    dp[i]=temp

    return temp

  

# finding overall longest path  

res = 0  

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

    res = max(res, dfs(i))

print(res)
import sys

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



sys.setrecursionlimit(10**9)



vertices = [[] for i in range(N+1)]

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



for _ in range(M):

    fro, to = list(map(int, input().split()))

    vertices[fro].append(to)



def dfs(i):

    

    if dp[i] != -1:

        return dp[i]

    

    temp = 0

    for vertex in vertices[i]:

        temp = max(temp, dfs(vertex)+1)

        

    dp[i] = temp

    return temp



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

    dfs(i)

    

print((max(dp)))
import sys

sys.setrecursionlimit(10**9)
n, m = list(map(int, input().split()))
g = [[] for i in range(n + 1)]
dp = [-1] * (n + 1)
for _ in range(m):
    v1, v2 = list(map(int, input().split()))
    g[v1].append(v2)
# finding longest path for vertice i
def dfs(i):
    if dp[i] != -1:
        return dp[i]
    temp = 0
    for nv in g[i]:
        temp = max(temp, dfs(nv) + 1)
    dp[i] = temp
    return temp


# finding overall longest path
res = 0
for i in range(1, n + 1):
    res = max(res, dfs(i))
print(res)

import sys

N, M = list(map(int, input().split()))
sys.setrecursionlimit(10**9)
vertices = [[] for i in range(N + 1)]
dp = [-1] * (N + 1)
for _ in range(M):
    fro, to = list(map(int, input().split()))
    vertices[fro].append(to)


def dfs(i):
    if dp[i] != -1:
        return dp[i]
    temp = 0
    for vertex in vertices[i]:
        temp = max(temp, dfs(vertex) + 1)
    dp[i] = temp
    return temp


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

+N, M = list(map(int, input().split())) -n, m = list(map(int, input().split())) -g = [[] for i in range(n + 1)] -dp = [-1] * (n + 1) -for _ in range(m): -    v1, v2 = list(map(int, input().split())) -    g[v1].append(v2) -# finding longest path for vertice i +vertices = [[] for i in range(N + 1)] +dp = [-1] * (N + 1) +for _ in range(M): +    fro, to = list(map(int, input().split())) +    vertices[fro].append(to) + + -    for nv in g[i]: -        temp = max(temp, dfs(nv) + 1) +    for vertex in vertices[i]: +        temp = max(temp, dfs(vertex) + 1) -# finding overall longest path -res = 0 -for i in range(1, n + 1): -    res = max(res, dfs(i)) -print(res) +for i in range(1, N + 1): +    dfs(i) +print((max(dp)))