import sys

from collections import deque



input = sys.stdin.readline



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

edge = [list(map(int,input().split())) for _ in range(M)]



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

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



for i in range(M):

    graph[edge[i][0]-1].append(edge[i][1]-1)

    indegree[edge[i][1]-1] += 1



queue = deque()

for i in range(N):

    if indegree[i] == 0:

        queue.append(i)



dp = [0]*N



while queue:

    node = queue.popleft()

    for adj in graph[node]:

        indegree[adj] -= 1

        if indegree[adj] == 0:

            queue.append(adj)

            dp[adj] = dp[node]+1



print((max(dp)))
import sys

input = sys.stdin.buffer.readline



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

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

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

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



for _ in range(M):

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

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

    deg[y - 1] += 1



stack = []



for i in range(N):

    if not deg[i]:

        stack.append(i)



while stack:

    node = stack.pop()

    for adj in graph[node]:

        deg[adj] -= 1

        dp[adj] = max(dp[adj], dp[node] + 1)

        if not deg[adj]:

            stack.append(adj)



print((max(dp)))
import sys
from collections import deque

input = sys.stdin.readline
N, M = list(map(int, input().split()))
edge = [list(map(int, input().split())) for _ in range(M)]
graph = [[] for _ in range(N)]
indegree = [0 for _ in range(N)]
for i in range(M):
    graph[edge[i][0] - 1].append(edge[i][1] - 1)
    indegree[edge[i][1] - 1] += 1
queue = deque()
for i in range(N):
    if indegree[i] == 0:
        queue.append(i)
dp = [0] * N
while queue:
    node = queue.popleft()
    for adj in graph[node]:
        indegree[adj] -= 1
        if indegree[adj] == 0:
            queue.append(adj)
            dp[adj] = dp[node] + 1
print((max(dp)))

import sys

input = sys.stdin.buffer.readline
N, M = list(map(int, input().split()))
graph = [[] for _ in range(N)]
deg = [0 for _ in range(N)]
dp = [0 for _ in range(N)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    graph[x - 1].append(y - 1)
    deg[y - 1] += 1
stack = []
for i in range(N):
    if not deg[i]:
        stack.append(i)
while stack:
    node = stack.pop()
    for adj in graph[node]:
        deg[adj] -= 1
        dp[adj] = max(dp[adj], dp[node] + 1)
        if not deg[adj]:
            stack.append(adj)
print((max(dp)))

-from collections import deque -input = sys.stdin.readline +input = sys.stdin.buffer.readline -edge = [list(map(int, input().split())) for _ in range(M)] -indegree = [0 for _ in range(N)] -for i in range(M): -    graph[edge[i][0] - 1].append(edge[i][1] - 1) -    indegree[edge[i][1] - 1] += 1 -queue = deque() +deg = [0 for _ in range(N)] +dp = [0 for _ in range(N)] +for _ in range(M): +    x, y = list(map(int, input().split())) +    graph[x - 1].append(y - 1) +    deg[y - 1] += 1 +stack = [] -    if indegree[i] == 0: -        queue.append(i) -dp = [0] * N -while queue: -    node = queue.popleft() +    if not deg[i]: +        stack.append(i) +while stack: +    node = stack.pop() -        indegree[adj] -= 1 -        if indegree[adj] == 0: -            queue.append(adj) -            dp[adj] = dp[node] + 1 +        deg[adj] -= 1 +        dp[adj] = max(dp[adj], dp[node] + 1) +        if not deg[adj]: +            stack.append(adj)