import sys



sys.setrecursionlimit(10 ** 5 * 2)

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)]

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)
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)]
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)))

+input = sys.stdin.readline