import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)



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

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

for _ in range(M):

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

    x -= 1

    y -= 1

    ad[y].append(x)



dp = [0]*N



def rec(v):

    if dp[v] != 0:

        return dp[v]

    res = 0

    for nv in ad[v]:

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

    dp[v] = res

    return dp[v]



for v in range(N):

    rec(v)



ans = max(dp)

print(ans)
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**6)



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

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

for _ in range(M):

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

    x -= 1

    y -= 1

    ad[y].append(x)



dp = [-1]*N



def rec(v):

    if dp[v] != -1:

        return dp[v]

    res = 0

    for nv in ad[v]:

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

    dp[v] = res

    return dp[v]



for v in range(N):

    rec(v)



ans = max(dp)

print(ans)
import sys

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


def rec(v):
    if dp[v] != 0:
        return dp[v]
    res = 0
    for nv in ad[v]:
        res = max(res, rec(nv) + 1)
    dp[v] = res
    return dp[v]


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

import sys

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


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


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

-dp = [0] * N +dp = [-1] * N -    if dp[v] != 0: +    if dp[v] != -1: