import sys



sys.setrecursionlimit(10 ** 8)



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

graph = [dict() for _ in range(N)]



for i in range(M):

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

    graph[y - 1][x - 1] = 1



dp = [0 for _ in range(N)]





def dfs(u):

    if dp[u] != 0:

        return dp[u]

    max_dis = 0

    for f in list(graph[u].keys()):

        if max_dis < dfs(f) + 1:

            max_dis = dfs(f) + 1



    dp[u] = max_dis

    return dp[u]





for i in range(N):

    dfs(i)

print((max(dp)))

import sys



sys.setrecursionlimit(10 ** 8)



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

graph = [dict() for _ in range(N)]



for i in range(M):

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

    graph[y - 1][x - 1] = 1



dp = [0 for _ in range(N)]





def dfs(u):

    if dp[u] != 0 or len(graph[u]) == 0:

        return dp[u]

    max_dis = 0

    for f in list(graph[u].keys()):

        if max_dis < dfs(f) + 1:

            max_dis = dfs(f) + 1



    dp[u] = max_dis

    return dp[u]





for i in range(N):

    dfs(i)

print((max(dp)))

import sys

sys.setrecursionlimit(10**8)
N, M = list(map(int, input().split()))
graph = [dict() for _ in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    graph[y - 1][x - 1] = 1
dp = [0 for _ in range(N)]


def dfs(u):
    if dp[u] != 0:
        return dp[u]
    max_dis = 0
    for f in list(graph[u].keys()):
        if max_dis < dfs(f) + 1:
            max_dis = dfs(f) + 1
    dp[u] = max_dis
    return dp[u]


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

import sys

sys.setrecursionlimit(10**8)
N, M = list(map(int, input().split()))
graph = [dict() for _ in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    graph[y - 1][x - 1] = 1
dp = [0 for _ in range(N)]


def dfs(u):
    if dp[u] != 0 or len(graph[u]) == 0:
        return dp[u]
    max_dis = 0
    for f in list(graph[u].keys()):
        if max_dis < dfs(f) + 1:
            max_dis = dfs(f) + 1
    dp[u] = max_dis
    return dp[u]


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

-    if dp[u] != 0: +    if dp[u] != 0 or len(graph[u]) == 0: