import sys

sys.setrecursionlimit(10 ** 7)

input = sys.stdin.readline



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



g = [[] for _ in range(n)]

indeg = [0 for _ in range(n)]



for _ in range(m):

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

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

    indeg[y-1] += 1



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





def LP(v):

    if dp[v] != -1:

        return dp[v]

    ans = 0

    for w in g[v]:

        ans = max(ans, LP(w)+1)

    dp[v] = ans

    return dp[v]





for i in range(n):

    LP(i)



print((max(dp)))

from collections import deque

import sys

sys.setrecursionlimit(10 ** 7)

input = sys.stdin.readline



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



g = [[] for _ in range(n)]

indeg = [0 for _ in range(n)]



for _ in range(m):

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

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

    indeg[y-1] += 1



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

q = deque([])



for i in range(n):

    if indeg[i] == 0:

        q.append(i)



while q:

    v = q.popleft()

    for w in g[v]:

        indeg[w] -= 1

        if indeg[w] == 0:

            q.append(w)

            dp[w] = max(dp[w], dp[v]+1)



print((max(dp)))

import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n, m = list(map(int, input().split()))
g = [[] for _ in range(n)]
indeg = [0 for _ in range(n)]
for _ in range(m):
    x, y = list(map(int, input().split()))
    g[x - 1].append(y - 1)
    indeg[y - 1] += 1
dp = [-1 for _ in range(n)]


def LP(v):
    if dp[v] != -1:
        return dp[v]
    ans = 0
    for w in g[v]:
        ans = max(ans, LP(w) + 1)
    dp[v] = ans
    return dp[v]


for i in range(n):
    LP(i)
print((max(dp)))

from collections import deque
import sys

sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n, m = list(map(int, input().split()))
g = [[] for _ in range(n)]
indeg = [0 for _ in range(n)]
for _ in range(m):
    x, y = list(map(int, input().split()))
    g[x - 1].append(y - 1)
    indeg[y - 1] += 1
dp = [0 for _ in range(n)]
q = deque([])
for i in range(n):
    if indeg[i] == 0:
        q.append(i)
while q:
    v = q.popleft()
    for w in g[v]:
        indeg[w] -= 1
        if indeg[w] == 0:
            q.append(w)
            dp[w] = max(dp[w], dp[v] + 1)
print((max(dp)))

+from collections import deque -dp = [-1 for _ in range(n)] - - -def LP(v): -    if dp[v] != -1: -        return dp[v] -    ans = 0 +dp = [0 for _ in range(n)] +q = deque([]) +for i in range(n): +    if indeg[i] == 0: +        q.append(i) +while q: +    v = q.popleft() -        ans = max(ans, LP(w) + 1) -    dp[v] = ans -    return dp[v] - - -for i in range(n): -    LP(i) +        indeg[w] -= 1 +        if indeg[w] == 0: +            q.append(w) +            dp[w] = max(dp[w], dp[v] + 1)