import sys

from functools import lru_cache

sys.setrecursionlimit(524287)



n,m,*l = list(map(int,open(0).read().split()))

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

for x,y in zip(l[::2],l[1::2]):

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



INF = float("inf")

done = [-INF]*n



@lru_cache(maxsize = None)

def dfs(t):

	if done[t] >= 0:

		pass

	elif pas[t]:

		done[t] = max(dfs(s) for s in pas[t]) + 1

	else:

		done[t] = 0

	return done[t]



print((max(dfs(i) for i in range(n))))
import sys

sys.setrecursionlimit(524287)



n,m,*l = list(map(int,open(0).read().split()))

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

for x,y in zip(l[::2],l[1::2]):

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



INF = float("inf")

done = [-INF]*n



def dfs(t):

	if done[t] >= 0:

		pass

	elif pas[t]:

		done[t] = max(dfs(s) for s in pas[t]) + 1

	else:

		done[t] = 0

	return done[t]



print((max(dfs(i) for i in range(n))))

import sys
from functools import lru_cache

sys.setrecursionlimit(524287)
n, m, *l = list(map(int, open(0).read().split()))
pas = [[] for _ in range(n)]
for x, y in zip(l[::2], l[1::2]):
    pas[x - 1].append(y - 1)
INF = float("inf")
done = [-INF] * n


@lru_cache(maxsize=None)
def dfs(t):
    if done[t] >= 0:
        pass
    elif pas[t]:
        done[t] = max(dfs(s) for s in pas[t]) + 1
    else:
        done[t] = 0
    return done[t]


print((max(dfs(i) for i in range(n))))

import sys

sys.setrecursionlimit(524287)
n, m, *l = list(map(int, open(0).read().split()))
pas = [[] for _ in range(n)]
for x, y in zip(l[::2], l[1::2]):
    pas[x - 1].append(y - 1)
INF = float("inf")
done = [-INF] * n


def dfs(t):
    if done[t] >= 0:
        pass
    elif pas[t]:
        done[t] = max(dfs(s) for s in pas[t]) + 1
    else:
        done[t] = 0
    return done[t]


print((max(dfs(i) for i in range(n))))

-from functools import lru_cache -@lru_cache(maxsize=None)