from sys import setrecursionlimit

setrecursionlimit(10 ** 9)



n, m = [int(i) for i in input().split()]

X = [[int(i) for i in input().split()] for j in range(m)]



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



for a, b in X:

    E[a].append(b)



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



def dfs(i):

    if memo[i] != -1:

        return memo[i] + 1

    if not E[i]:

        return 0



    memo[i] = max(dfs(e) for e in E[i])

    return memo[i] + 1



print((max(dfs(i) for i in range(1, n+1))))
from collections import deque



n, m = [int(i) for i in input().split()]

X = [[int(i) for i in input().split()] for j in range(m)]



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

IN = [0] * (n+1)



for a, b in X:

    E[a].append(b)

    IN[b] += 1



q = deque((i, 0) for i in range(1, n+1) if IN[i] == 0)

D = [0] * (n+1)



while q:

    i, d = q.popleft()

    IN[i] -= 1

    if IN[i] > 0:

        continue

    D[i] = d

    for e in E[i]:

        q.append((e, d+1))



print((max(D)))
from sys import setrecursionlimit

setrecursionlimit(10**9)
n, m = [int(i) for i in input().split()]
X = [[int(i) for i in input().split()] for j in range(m)]
E = [[] for i in range(n + 1)]
for a, b in X:
    E[a].append(b)
memo = [-1] * (n + 1)


def dfs(i):
    if memo[i] != -1:
        return memo[i] + 1
    if not E[i]:
        return 0
    memo[i] = max(dfs(e) for e in E[i])
    return memo[i] + 1


print((max(dfs(i) for i in range(1, n + 1))))

from collections import deque

n, m = [int(i) for i in input().split()]
X = [[int(i) for i in input().split()] for j in range(m)]
E = [[] for i in range(n + 1)]
IN = [0] * (n + 1)
for a, b in X:
    E[a].append(b)
    IN[b] += 1
q = deque((i, 0) for i in range(1, n + 1) if IN[i] == 0)
D = [0] * (n + 1)
while q:
    i, d = q.popleft()
    IN[i] -= 1
    if IN[i] > 0:
        continue
    D[i] = d
    for e in E[i]:
        q.append((e, d + 1))
print((max(D)))

-from sys import setrecursionlimit +from collections import deque -setrecursionlimit(10**9) +IN = [0] * (n + 1) -memo = [-1] * (n + 1) - - -def dfs(i): -    if memo[i] != -1: -        return memo[i] + 1 -    if not E[i]: -        return 0 -    memo[i] = max(dfs(e) for e in E[i]) -    return memo[i] + 1 - - -print((max(dfs(i) for i in range(1, n + 1)))) +    IN[b] += 1 +q = deque((i, 0) for i in range(1, n + 1) if IN[i] == 0) +D = [0] * (n + 1) +while q: +    i, d = q.popleft() +    IN[i] -= 1 +    if IN[i] > 0: +        continue +    D[i] = d +    for e in E[i]: +        q.append((e, d + 1)) +print((max(D)))