import sys

sys.setrecursionlimit(10**6)

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

G=[set() for i in range(N)]

for i in range(M):

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

    G[x-1].add(y-1)

dp=[-1 for i in range(N)]

def search(k):

    if dp[k]!=-1:

        return dp[k]

    res=0

    for r in G[k]:

        if search(r)+1>res:

            res=search(r)+1

    dp[k]=res

    return res

for i in range(N):

    if dp[i]==-1:

        search(i)

print((max(dp)))

from collections import deque

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

G=[set() for i in range(N)]

for i in range(M):

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

    G[x-1].add(y-1)

K=[0 for i in range(N)]

for i in range(N):

    for p in G[i]:

        K[p]+=1

q=deque(i for i in range(N) if K[i]==0)

res=[]

while q:

    v1=q.popleft()

    res.append(v1)

    for v2 in G[v1]:

        K[v2]-=1

        if K[v2]==0:

            q.append(v2)

dp=[0 for i in range(N)]

for i in range(N-1,-1,-1):

    v=res[i]

    for p in G[v]:

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

print((max(dp)))
import sys

sys.setrecursionlimit(10**6)
N, M = list(map(int, input().split()))
G = [set() for i in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    G[x - 1].add(y - 1)
dp = [-1 for i in range(N)]


def search(k):
    if dp[k] != -1:
        return dp[k]
    res = 0
    for r in G[k]:
        if search(r) + 1 > res:
            res = search(r) + 1
    dp[k] = res
    return res


for i in range(N):
    if dp[i] == -1:
        search(i)
print((max(dp)))

from collections import deque

N, M = list(map(int, input().split()))
G = [set() for i in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    G[x - 1].add(y - 1)
K = [0 for i in range(N)]
for i in range(N):
    for p in G[i]:
        K[p] += 1
q = deque(i for i in range(N) if K[i] == 0)
res = []
while q:
    v1 = q.popleft()
    res.append(v1)
    for v2 in G[v1]:
        K[v2] -= 1
        if K[v2] == 0:
            q.append(v2)
dp = [0 for i in range(N)]
for i in range(N - 1, -1, -1):
    v = res[i]
    for p in G[v]:
        dp[v] = max(dp[v], dp[p] + 1)
print((max(dp)))

-import sys +from collections import deque -sys.setrecursionlimit(10**6) -dp = [-1 for i in range(N)] - - -def search(k): -    if dp[k] != -1: -        return dp[k] -    res = 0 -    for r in G[k]: -        if search(r) + 1 > res: -            res = search(r) + 1 -    dp[k] = res -    return res - - +K = [0 for i in range(N)] -    if dp[i] == -1: -        search(i) +    for p in G[i]: +        K[p] += 1 +q = deque(i for i in range(N) if K[i] == 0) +res = [] +while q: +    v1 = q.popleft() +    res.append(v1) +    for v2 in G[v1]: +        K[v2] -= 1 +        if K[v2] == 0: +            q.append(v2) +dp = [0 for i in range(N)] +for i in range(N - 1, -1, -1): +    v = res[i] +    for p in G[v]: +        dp[v] = max(dp[v], dp[p] + 1)