from sys import setrecursionlimit

setrecursionlimit(10 ** 9)



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

xys = [list(map(int, input().split())) for _  in range(M)]



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

for x, y in xys:

    children[x-1].append(y-1)



has_no_parent = [True] * N

has_no_child = [True] * N

for x, y in xys:

    has_no_child[x-1] = False

    has_no_parent[y-1] = False



lp = [None] * N

for node in range(N):

    if has_no_child[node]:

        lp[node] = 0



def dp(node):

    if lp[node] is not None:

        return lp[node]

    result = max([dp(child) + 1 for child in children[node]])

    lp[node] = result

    return result



for node in range(N):

    if lp[node] is None:

        dp(node)



print((max(lp)))

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

xys = [list(map(int, input().split())) for _  in range(M)]



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

for x, y in xys:

    children[x-1].append(y-1)



has_no_parent = [True] * N

has_no_child = [True] * N

for x, y in xys:

    has_no_child[x-1] = False

    has_no_parent[y-1] = False



dp = [None] * N

for node in range(N):

    if has_no_child[node]:

        dp[node] = 0



for node in range(N):

    if dp[node] is not None:

        continue

    stack = []

    stack.append(node)

    while stack:

        parent = stack[-1]

        all_children_defined = True

        for child in children[parent]:

            if dp[child] is None:

                stack.append(child)

                all_children_defined = False

        if all_children_defined:

            stack.pop()

            dp[parent] = max([dp[child] + 1 for child in children[parent]])



print((max(dp)))

from sys import setrecursionlimit

setrecursionlimit(10**9)
N, M = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(M)]
children = [[] for _ in range(N)]
for x, y in xys:
    children[x - 1].append(y - 1)
has_no_parent = [True] * N
has_no_child = [True] * N
for x, y in xys:
    has_no_child[x - 1] = False
    has_no_parent[y - 1] = False
lp = [None] * N
for node in range(N):
    if has_no_child[node]:
        lp[node] = 0


def dp(node):
    if lp[node] is not None:
        return lp[node]
    result = max([dp(child) + 1 for child in children[node]])
    lp[node] = result
    return result


for node in range(N):
    if lp[node] is None:
        dp(node)
print((max(lp)))

N, M = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(M)]
children = [[] for _ in range(N)]
for x, y in xys:
    children[x - 1].append(y - 1)
has_no_parent = [True] * N
has_no_child = [True] * N
for x, y in xys:
    has_no_child[x - 1] = False
    has_no_parent[y - 1] = False
dp = [None] * N
for node in range(N):
    if has_no_child[node]:
        dp[node] = 0
for node in range(N):
    if dp[node] is not None:
        continue
    stack = []
    stack.append(node)
    while stack:
        parent = stack[-1]
        all_children_defined = True
        for child in children[parent]:
            if dp[child] is None:
                stack.append(child)
                all_children_defined = False
        if all_children_defined:
            stack.pop()
            dp[parent] = max([dp[child] + 1 for child in children[parent]])
print((max(dp)))

-from sys import setrecursionlimit - -setrecursionlimit(10**9) -lp = [None] * N +dp = [None] * N -        lp[node] = 0 - - -def dp(node): -    if lp[node] is not None: -        return lp[node] -    result = max([dp(child) + 1 for child in children[node]]) -    lp[node] = result -    return result - - +        dp[node] = 0 -    if lp[node] is None: -        dp(node) -print((max(lp))) +    if dp[node] is not None: +        continue +    stack = [] +    stack.append(node) +    while stack: +        parent = stack[-1] +        all_children_defined = True +        for child in children[parent]: +            if dp[child] is None: +                stack.append(child) +                all_children_defined = False +        if all_children_defined: +            stack.pop() +            dp[parent] = max([dp[child] + 1 for child in children[parent]]) +print((max(dp)))