import sys

sys.setrecursionlimit(10 ** 5 + 5)

input = sys.stdin.readline

'''

allinputs = iter(input().splitlines())

input = lambda : next(allinputs)

#'''

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

x, y = [0] * M, [0] * M

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

memo = [-1] * N

ans = 0



def length(i):

	if memo[i] >= 0:

		return memo[i]

	else:

		if len(from_x[i]) == 0:

			memo[i] = 0

			return memo[i]

		else:

			ret = 0

			

			for j in from_x[i]:

				tmp = length(j) + 1

				if tmp > ret:

					ret = tmp

					

			memo[i] = ret

			return ret



for i in range(M):

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

	from_x[y[i] - 1].append(x[i] - 1)



for i in range(N):

	tmp = length(i)

	if tmp > ans:

		ans = tmp



print(ans)

import sys

input = sys.stdin.readline

'''

allinputs = iter(input().splitlines())

input = lambda : next(allinputs)

#'''

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

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

deg = [0] * N

stack = []

dp = [0] * N



for _ in range(M):

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

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

	deg[y - 1] += 1

	

for i in range(N):

	if deg[i] == 0:

		stack.append(i)

		

while stack:

	x = stack.pop(-1)

	for y in to_y[x]:

		dp[y] = max(dp[y], dp[x] + 1)

		deg[y] -= 1

		if deg[y] == 0:

			stack.append(y)

			

print((max(dp)))
import sys

sys.setrecursionlimit(10**5 + 5)
input = sys.stdin.readline
"""
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#"""
N, M = list(map(int, input().split()))
x, y = [0] * M, [0] * M
from_x = [[] for _ in range(N)]
memo = [-1] * N
ans = 0


def length(i):
    if memo[i] >= 0:
        return memo[i]
    else:
        if len(from_x[i]) == 0:
            memo[i] = 0
            return memo[i]
        else:
            ret = 0
            for j in from_x[i]:
                tmp = length(j) + 1
                if tmp > ret:
                    ret = tmp
            memo[i] = ret
            return ret


for i in range(M):
    x[i], y[i] = list(map(int, input().split()))
    from_x[y[i] - 1].append(x[i] - 1)
for i in range(N):
    tmp = length(i)
    if tmp > ans:
        ans = tmp
print(ans)

import sys

input = sys.stdin.readline
"""
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#"""
N, M = list(map(int, input().split()))
to_y = [[] for _ in range(N)]
deg = [0] * N
stack = []
dp = [0] * N
for _ in range(M):
    x, y = list(map(int, input().split()))
    to_y[x - 1].append(y - 1)
    deg[y - 1] += 1
for i in range(N):
    if deg[i] == 0:
        stack.append(i)
while stack:
    x = stack.pop(-1)
    for y in to_y[x]:
        dp[y] = max(dp[y], dp[x] + 1)
        deg[y] -= 1
        if deg[y] == 0:
            stack.append(y)
print((max(dp)))

-sys.setrecursionlimit(10**5 + 5) -x, y = [0] * M, [0] * M -from_x = [[] for _ in range(N)] -memo = [-1] * N -ans = 0 - - -def length(i): -    if memo[i] >= 0: -        return memo[i] -    else: -        if len(from_x[i]) == 0: -            memo[i] = 0 -            return memo[i] -        else: -            ret = 0 -            for j in from_x[i]: -                tmp = length(j) + 1 -                if tmp > ret: -                    ret = tmp -            memo[i] = ret -            return ret - - -for i in range(M): -    x[i], y[i] = list(map(int, input().split())) -    from_x[y[i] - 1].append(x[i] - 1) +to_y = [[] for _ in range(N)] +deg = [0] * N +stack = [] +dp = [0] * N +for _ in range(M): +    x, y = list(map(int, input().split())) +    to_y[x - 1].append(y - 1) +    deg[y - 1] += 1 -    tmp = length(i) -    if tmp > ans: -        ans = tmp -print(ans) +    if deg[i] == 0: +        stack.append(i) +while stack: +    x = stack.pop(-1) +    for y in to_y[x]: +        dp[y] = max(dp[y], dp[x] + 1) +        deg[y] -= 1 +        if deg[y] == 0: +            stack.append(y) +print((max(dp)))