import sys

import collections

from functools import lru_cache



xy = []

d = collections.defaultdict(list)

@lru_cache(maxsize=None)

def alg_memoization_recursion(f):

    mt = 0

    for v in d[f]:

        mt = max(mt, alg_memoization_recursion(v) + 1)

    return mt





def solve():

    readline = sys.stdin.buffer.readline

    mod = 10 ** 9 + 7

    sys.setrecursionlimit(10 ** 5 + 1)

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

    for i in range(m):

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

        d[x-1] += [y-1]

    mt = 0

    for i in range(n):

        mt = max(mt, alg_memoization_recursion(i))

    print(mt)





if __name__ == '__main__':

    solve()

import sys

import collections

from functools import lru_cache



sys.setrecursionlimit(10 ** 7 + 1)

xy = []

d = collections.defaultdict(list)



@lru_cache(maxsize=None)

def alg_memoization_recursion(f):

    mt = 0

    for v in d[f]:

        mt = max(mt, alg_memoization_recursion(v) + 1)

    return mt





def solve():

    readline = sys.stdin.buffer.readline

    mod = 10 ** 9 + 7

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

    for i in range(m):

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

        d[x-1] += [y-1]

    mt = 0

    for i in range(n):

        mt = max(mt, alg_memoization_recursion(i))

    print(mt)





if __name__ == '__main__':

    solve()

import sys
import collections
from functools import lru_cache

xy = []
d = collections.defaultdict(list)


@lru_cache(maxsize=None)
def alg_memoization_recursion(f):
    mt = 0
    for v in d[f]:
        mt = max(mt, alg_memoization_recursion(v) + 1)
    return mt


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10**9 + 7
    sys.setrecursionlimit(10**5 + 1)
    n, m = list(map(int, readline().split()))
    for i in range(m):
        x, y = list(map(int, readline().split()))
        d[x - 1] += [y - 1]
    mt = 0
    for i in range(n):
        mt = max(mt, alg_memoization_recursion(i))
    print(mt)


if __name__ == "__main__":
    solve()

import sys
import collections
from functools import lru_cache

sys.setrecursionlimit(10**7 + 1)
xy = []
d = collections.defaultdict(list)


@lru_cache(maxsize=None)
def alg_memoization_recursion(f):
    mt = 0
    for v in d[f]:
        mt = max(mt, alg_memoization_recursion(v) + 1)
    return mt


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10**9 + 7
    n, m = list(map(int, readline().split()))
    for i in range(m):
        x, y = list(map(int, readline().split()))
        d[x - 1] += [y - 1]
    mt = 0
    for i in range(n):
        mt = max(mt, alg_memoization_recursion(i))
    print(mt)


if __name__ == "__main__":
    solve()

+sys.setrecursionlimit(10**7 + 1) -    sys.setrecursionlimit(10**5 + 1)