import sys

sys.setrecursionlimit(10**5+5)

input = sys.stdin.readline



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

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



for i in range(M):

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

    x-=1

    y-=1

    adj[x].append(y)

    

dp=[-1]*N#i番目絡みた最長経路

for i in range(N):

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

        dp[i]=0



def rec(ii):

    new=0

    for to in adj[ii]:

        if dp[to]!=-1:

            new=dp[to]

        else:

            new=rec(to)

        dp[ii]=max(new+1,dp[ii])

    return dp[ii]

        

for i in range(N):

    rec(i)

    

print((max(dp)))
import sys

sys.setrecursionlimit(10**5+5)

input = sys.stdin.readline



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

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



for i in range(M):

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

    x-=1

    y-=1

    adj[x].append(y)

    

dp=[0]*N#i番目絡みた最長経路



def rec(ii):

    new=0

    if len(adj[ii])==0:

        return 0

    for to in adj[ii]:

        if dp[to]!=0:

            new=dp[to]

        else:

            new=rec(to)

        dp[ii]=max(new+1,dp[ii])

    return dp[ii]

        

for i in range(N):

    rec(i)

    

print((max(dp)))
import sys

sys.setrecursionlimit(10**5 + 5)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    adj[x].append(y)
dp = [-1] * N  # i番目絡みた最長経路
for i in range(N):
    if len(adj[i]) == 0:
        dp[i] = 0


def rec(ii):
    new = 0
    for to in adj[ii]:
        if dp[to] != -1:
            new = dp[to]
        else:
            new = rec(to)
        dp[ii] = max(new + 1, dp[ii])
    return dp[ii]


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

import sys

sys.setrecursionlimit(10**5 + 5)
input = sys.stdin.readline
N, M = list(map(int, input().split()))
adj = [[] for _ in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    adj[x].append(y)
dp = [0] * N  # i番目絡みた最長経路


def rec(ii):
    new = 0
    if len(adj[ii]) == 0:
        return 0
    for to in adj[ii]:
        if dp[to] != 0:
            new = dp[to]
        else:
            new = rec(to)
        dp[ii] = max(new + 1, dp[ii])
    return dp[ii]


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

-dp = [-1] * N  # i番目絡みた最長経路 -for i in range(N): -    if len(adj[i]) == 0: -        dp[i] = 0 +dp = [0] * N  # i番目絡みた最長経路 +    if len(adj[ii]) == 0: +        return 0 -        if dp[to] != -1: +        if dp[to] != 0: