import sys

sys.setrecursionlimit(10 ** 9)



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

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

for i in range(M):

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

    x, y = x - 1, y - 1

    G[x].append(y)





def topological_sort(g):

    topological_list = []

    visited = set()



    def dfs(n):

        for e in g[n]:

            if e not in visited:

                dfs(e)

        topological_list.append(n)

        visited.add(n)



    for i in range(len(g)):

        if i not in visited:

            dfs(i)



    return topological_list[::-1]





T = topological_sort(G)

dist = [0] * N

for n in T:

    for v in G[n]:

        dist[v] = max(dist[v], dist[n] + 1)



print((max(dist)))

import sys

sys.setrecursionlimit(10 ** 9)



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

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

for i in range(M):

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

    x, y = x - 1, y - 1

    G[x].append(y)





def topological_sort(g):

    topological_list = []

    visited = set()



    def dfs(n):

        for e in g[n]:

            if e not in visited:

                dfs(e)

        topological_list.append(n)

        visited.add(n)



    for i in range(len(g)):

        if i not in visited:

            dfs(i)



    return topological_list[::-1]





topological_order = topological_sort(G)

dp = [0] * N

for t in topological_order:

    for to in G[t]:

        dp[to] = max(dp[to], dp[t] + 1)



print((max(dp)))

import sys

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


def topological_sort(g):
    topological_list = []
    visited = set()

    def dfs(n):
        for e in g[n]:
            if e not in visited:
                dfs(e)
        topological_list.append(n)
        visited.add(n)

    for i in range(len(g)):
        if i not in visited:
            dfs(i)
    return topological_list[::-1]


T = topological_sort(G)
dist = [0] * N
for n in T:
    for v in G[n]:
        dist[v] = max(dist[v], dist[n] + 1)
print((max(dist)))

import sys

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


def topological_sort(g):
    topological_list = []
    visited = set()

    def dfs(n):
        for e in g[n]:
            if e not in visited:
                dfs(e)
        topological_list.append(n)
        visited.add(n)

    for i in range(len(g)):
        if i not in visited:
            dfs(i)
    return topological_list[::-1]


topological_order = topological_sort(G)
dp = [0] * N
for t in topological_order:
    for to in G[t]:
        dp[to] = max(dp[to], dp[t] + 1)
print((max(dp)))

-T = topological_sort(G) -dist = [0] * N -for n in T: -    for v in G[n]: -        dist[v] = max(dist[v], dist[n] + 1) -print((max(dist))) +topological_order = topological_sort(G) +dp = [0] * N +for t in topological_order: +    for to in G[t]: +        dp[to] = max(dp[to], dp[t] + 1) +print((max(dp)))