import sys

sys.setrecursionlimit(10000)

# Longest path

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

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

for _ in range(M):

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

    graph[x-1].append(y-1)

dp = [-1 for _ in range(N)]



def rec(c):

    if dp[c] != -1:

        return dp[c]

    res = 0

    for i in graph[c]:

        res = max(res, rec(i) + 1)

    dp[c] = res

    return res



for i in range(N):

    rec(i)

print((max(dp)))
import sys

sys.setrecursionlimit(10000)



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

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

dp = [-1 for _ in range(N)]

for _ in range(M):

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

    graph[x-1].append(y-1)



def rec(v):

    if dp[v] >= 0:

        return dp[v]

    temp = 0

    for next in graph[v]:

        temp = max(rec(next) + 1, temp)

    dp[v] = temp

    return temp



for i in range(N):

    rec(i)

print((max(dp)))
import sys

sys.setrecursionlimit(10000)
# Longest path
N, M = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    graph[x - 1].append(y - 1)
dp = [-1 for _ in range(N)]


def rec(c):
    if dp[c] != -1:
        return dp[c]
    res = 0
    for i in graph[c]:
        res = max(res, rec(i) + 1)
    dp[c] = res
    return res


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

import sys

sys.setrecursionlimit(10000)
N, M = list(map(int, input().split()))
graph = [[] for _ in range(N)]
dp = [-1 for _ in range(N)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    graph[x - 1].append(y - 1)


def rec(v):
    if dp[v] >= 0:
        return dp[v]
    temp = 0
    for next in graph[v]:
        temp = max(rec(next) + 1, temp)
    dp[v] = temp
    return temp


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

-# Longest path +dp = [-1 for _ in range(N)] -dp = [-1 for _ in range(N)] -def rec(c): -    if dp[c] != -1: -        return dp[c] -    res = 0 -    for i in graph[c]: -        res = max(res, rec(i) + 1) -    dp[c] = res -    return res +def rec(v): +    if dp[v] >= 0: +        return dp[v] +    temp = 0 +    for next in graph[v]: +        temp = max(rec(next) + 1, temp) +    dp[v] = temp +    return temp