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

es = [tuple([int(x)-1 for x in input().split()]) for i in range(M)]



indeg = [0 for i in range(N)]

outdeg = [[] for i in range(N)]

for a,b in es:

    indeg[b] += 1

    outdeg[a].append(b)



heads = []

for i in range(N):

    if indeg[i]: continue

    heads.append(i)



dp = [0] * N

while heads:

    a = heads.pop()

    for b in outdeg[a]:

        indeg[b] -= 1

        if indeg[b] == 0:

            heads.append(b)

        dp[b] = max(dp[b], dp[a] + 1)

print((max(dp)))
import sys

input = sys.stdin.readline

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

XY = [tuple(map(int,input().split())) for i in range(M)]



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

indeg = [0] * N

for x,y in XY:

    x,y = x-1,y-1

    es[x].append(y)

    indeg[y] += 1



from collections import deque

q = deque()

for i in range(N):

    if indeg[i] == 0:

        q.append(i)



dp = [0] * N

while q:

    v = q.popleft()

    for to in es[v]:

        indeg[to] -= 1

        if indeg[to] == 0: q.append(to)

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



print((max(dp)))
N, M = list(map(int, input().split()))
es = [tuple([int(x) - 1 for x in input().split()]) for i in range(M)]
indeg = [0 for i in range(N)]
outdeg = [[] for i in range(N)]
for a, b in es:
    indeg[b] += 1
    outdeg[a].append(b)
heads = []
for i in range(N):
    if indeg[i]:
        continue
    heads.append(i)
dp = [0] * N
while heads:
    a = heads.pop()
    for b in outdeg[a]:
        indeg[b] -= 1
        if indeg[b] == 0:
            heads.append(b)
        dp[b] = max(dp[b], dp[a] + 1)
print((max(dp)))

import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
XY = [tuple(map(int, input().split())) for i in range(M)]
es = [[] for _ in range(N)]
indeg = [0] * N
for x, y in XY:
    x, y = x - 1, y - 1
    es[x].append(y)
    indeg[y] += 1
from collections import deque

q = deque()
for i in range(N):
    if indeg[i] == 0:
        q.append(i)
dp = [0] * N
while q:
    v = q.popleft()
    for to in es[v]:
        indeg[to] -= 1
        if indeg[to] == 0:
            q.append(to)
        dp[to] = max(dp[to], dp[v] + 1)
print((max(dp)))

+import sys + +input = sys.stdin.readline -es = [tuple([int(x) - 1 for x in input().split()]) for i in range(M)] -indeg = [0 for i in range(N)] -outdeg = [[] for i in range(N)] -for a, b in es: -    indeg[b] += 1 -    outdeg[a].append(b) -heads = [] +XY = [tuple(map(int, input().split())) for i in range(M)] +es = [[] for _ in range(N)] +indeg = [0] * N +for x, y in XY: +    x, y = x - 1, y - 1 +    es[x].append(y) +    indeg[y] += 1 +from collections import deque + +q = deque() -    if indeg[i]: -        continue -    heads.append(i) +    if indeg[i] == 0: +        q.append(i) -while heads: -    a = heads.pop() -    for b in outdeg[a]: -        indeg[b] -= 1 -        if indeg[b] == 0: -            heads.append(b) -        dp[b] = max(dp[b], dp[a] + 1) +while q: +    v = q.popleft() +    for to in es[v]: +        indeg[to] -= 1 +        if indeg[to] == 0: +            q.append(to) +        dp[to] = max(dp[to], dp[v] + 1)