import sys



sys.setrecursionlimit(10 ** 5 * 2)

input = sys.stdin.readline

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

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

for i in range(M):

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

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



mem = [-1] * N





def rec(n):

    if mem[n] > -1:

        return mem[n]

    elif len(Ps[n]) == 0:

        mem[n] = 0

        return mem[n]

    else:

        mem[n] = max([rec(Ps[n][i]) for i in range(len(Ps[n]))]) + 1

        return mem[n]





for i in range(N):

    rec(i)

print((max(mem)))
import sys



sys.setrecursionlimit(10 ** 5 * 2)

input = sys.stdin.readline

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

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

deg = [0] * N

for i in range(M):

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

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

    deg[y - 1] += 1



stack = []

for i in range(N):

    if deg[i] == 0:

        stack.append(i)



dp = [0] * N

while (stack):

    n = stack.pop()

    for p in Ps[n]:

        dp[p] = max(dp[p], dp[n] + 1)

        deg[p] -= 1

        if deg[p] == 0:

            stack.append(p)



print((max(dp)))

import sys

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


def rec(n):
    if mem[n] > -1:
        return mem[n]
    elif len(Ps[n]) == 0:
        mem[n] = 0
        return mem[n]
    else:
        mem[n] = max([rec(Ps[n][i]) for i in range(len(Ps[n]))]) + 1
        return mem[n]


for i in range(N):
    rec(i)
print((max(mem)))

import sys

sys.setrecursionlimit(10**5 * 2)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
Ps = [[] for _ in range(N)]
deg = [0] * N
for i in range(M):
    x, y = list(map(int, input().split()))
    Ps[x - 1].append(y - 1)
    deg[y - 1] += 1
stack = []
for i in range(N):
    if deg[i] == 0:
        stack.append(i)
dp = [0] * N
while stack:
    n = stack.pop()
    for p in Ps[n]:
        dp[p] = max(dp[p], dp[n] + 1)
        deg[p] -= 1
        if deg[p] == 0:
            stack.append(p)
print((max(dp)))

+deg = [0] * N -mem = [-1] * N - - -def rec(n): -    if mem[n] > -1: -        return mem[n] -    elif len(Ps[n]) == 0: -        mem[n] = 0 -        return mem[n] -    else: -        mem[n] = max([rec(Ps[n][i]) for i in range(len(Ps[n]))]) + 1 -        return mem[n] - - +    deg[y - 1] += 1 +stack = [] -    rec(i) -print((max(mem))) +    if deg[i] == 0: +        stack.append(i) +dp = [0] * N +while stack: +    n = stack.pop() +    for p in Ps[n]: +        dp[p] = max(dp[p], dp[n] + 1) +        deg[p] -= 1 +        if deg[p] == 0: +            stack.append(p) +print((max(dp)))