import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

n, m = list(map(int, input().split()))

edges = [[] for _ in range(n)]

for _ in range(m):

    x, y = list([int(x)-1 for x in input().split()])

    edges[x].append(y)





def memoize(f):

    memo = dict()



    def memoized(*args):

        if args in list(memo.keys()):

            return memo[args]

        else:

            memo[args] = f(*args)

            return memo[args]

    return memoized



@memoize

def longest_path(v):

    retval = 0

    for v2 in edges[v]:

        retval = max(retval, longest_path(v2)+1)

    return retval





ans = 0

for i in range(n):

    ans = max(ans, longest_path(i))

print(ans)
import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

n, m = list(map(int, input().split()))

edges = [[] for _ in range(n)]

for _ in range(m):

    x, y = list([int(x)-1 for x in input().split()])

    edges[x].append(y)



dist = [-1]*n





def longest_path(v):

    if dist[v] != -1:

        return dist[v]

    retval = 0

    for v2 in edges[v]:

        retval = max(retval, longest_path(v2)+1)

    dist[v] = retval

    return dist[v]





ans = 0

for i in range(n):

    ans = max(ans, longest_path(i))

print(ans)
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(m):
    x, y = list([int(x) - 1 for x in input().split()])
    edges[x].append(y)


def memoize(f):
    memo = dict()

    def memoized(*args):
        if args in list(memo.keys()):
            return memo[args]
        else:
            memo[args] = f(*args)
            return memo[args]

    return memoized


@memoize
def longest_path(v):
    retval = 0
    for v2 in edges[v]:
        retval = max(retval, longest_path(v2) + 1)
    return retval


ans = 0
for i in range(n):
    ans = max(ans, longest_path(i))
print(ans)

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(m):
    x, y = list([int(x) - 1 for x in input().split()])
    edges[x].append(y)
dist = [-1] * n


def longest_path(v):
    if dist[v] != -1:
        return dist[v]
    retval = 0
    for v2 in edges[v]:
        retval = max(retval, longest_path(v2) + 1)
    dist[v] = retval
    return dist[v]


ans = 0
for i in range(n):
    ans = max(ans, longest_path(i))
print(ans)

+dist = [-1] * n -def memoize(f): -    memo = dict() - -    def memoized(*args): -        if args in list(memo.keys()): -            return memo[args] -        else: -            memo[args] = f(*args) -            return memo[args] - -    return memoized - - -@memoize +    if dist[v] != -1: +        return dist[v] -    return retval +    dist[v] = retval +    return dist[v]