import sys



sys.setrecursionlimit(10**5+10)

input = sys.stdin.readline



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

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

for _ in range(M):

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

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



dp = [-1] * N



def rec(v):

	if dp[v] != -1:

		return dp[v]

	ans = 0

	lst_nv = lst_edge[v]

	for nv in lst_nv:

		ans = max(ans, rec(nv)+1)

	dp[v] = ans

	return dp[v]



ans = 0

for v in range(N):

	ans = max(ans,rec(v))

print(ans)
import sys

import collections



input = sys.stdin.readline



def chmax(a,b):

	if a>b : 

		return a

	return b



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

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

deg = [0]*N

for _ in range(M):

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

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

	deg[y-1] += 1



que = collections.deque()

for v in range(N):

	if deg[v] == 0:

		que.append(v)



dp = [0]*N



while que:

	v = que.popleft()

	for nv in lst_edge[v]:

		deg[nv] -= 1

		if(deg[nv] == 0):

			que.append(nv)

			dp[nv] = chmax(dp[nv], dp[v]+1)

print((max(dp)))
import sys

sys.setrecursionlimit(10**5 + 10)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
lst_edge = [[] for _ in range(N)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    lst_edge[x - 1].append(y - 1)
dp = [-1] * N


def rec(v):
    if dp[v] != -1:
        return dp[v]
    ans = 0
    lst_nv = lst_edge[v]
    for nv in lst_nv:
        ans = max(ans, rec(nv) + 1)
    dp[v] = ans
    return dp[v]


ans = 0
for v in range(N):
    ans = max(ans, rec(v))
print(ans)

import sys
import collections

input = sys.stdin.readline


def chmax(a, b):
    if a > b:
        return a
    return b


N, M = list(map(int, input().split()))
lst_edge = [[] for _ in range(N)]
deg = [0] * N
for _ in range(M):
    x, y = list(map(int, input().split()))
    lst_edge[x - 1].append(y - 1)
    deg[y - 1] += 1
que = collections.deque()
for v in range(N):
    if deg[v] == 0:
        que.append(v)
dp = [0] * N
while que:
    v = que.popleft()
    for nv in lst_edge[v]:
        deg[nv] -= 1
        if deg[nv] == 0:
            que.append(nv)
            dp[nv] = chmax(dp[nv], dp[v] + 1)
print((max(dp)))

+import collections -sys.setrecursionlimit(10**5 + 10) + + +def chmax(a, b): +    if a > b: +        return a +    return b + + +deg = [0] * N -dp = [-1] * N - - -def rec(v): -    if dp[v] != -1: -        return dp[v] -    ans = 0 -    lst_nv = lst_edge[v] -    for nv in lst_nv: -        ans = max(ans, rec(nv) + 1) -    dp[v] = ans -    return dp[v] - - -ans = 0 +    deg[y - 1] += 1 +que = collections.deque() -    ans = max(ans, rec(v)) -print(ans) +    if deg[v] == 0: +        que.append(v) +dp = [0] * N +while que: +    v = que.popleft() +    for nv in lst_edge[v]: +        deg[nv] -= 1 +        if deg[nv] == 0: +            que.append(nv) +            dp[nv] = chmax(dp[nv], dp[v] + 1) +print((max(dp)))