n, m = [ int(v) for v in input().split() ]

edge = []

in_list = [0] * n

out_list = [0] * n

dp_list = [0] * n

connect_list = [ [] for i in range(n) ] 



for i in range(m):

    s, t = [ int(v)-1 for v in input().split() ]

    edge.append((s,t))

    in_list[t] += 1

    out_list[s] += 1

    connect_list[t].append(s)



search_list = [ i for i, v in enumerate(out_list) if v == 0 ]

while search_list:

    new_search_list = []

    for i in search_list:

        for j in connect_list[i]:

            out_list[j] -= 1

            if dp_list[j] < dp_list[i] + 1:

                dp_list[j] = dp_list[i] + 1

            if out_list[j] == 0:

                new_search_list.append(j)

    search_list = new_search_list



print((max(dp_list)))

import bisect, collections, copy, heapq, itertools, math, sys

input = sys.stdin.readline

n, m = [ int(v) for v in input().split() ]

edge = []

in_list = [0] * n

out_list = [0] * n

dp_list = [0] * n

connect_list = [ [] for i in range(n) ] 



for i in range(m):

    s, t = [ int(v)-1 for v in input().split() ]

    edge.append((s,t))

    in_list[t] += 1

    out_list[s] += 1

    connect_list[t].append(s)



search_list = [ i for i, v in enumerate(out_list) if v == 0 ]

while search_list:

    new_search_list = []

    for i in search_list:

        for j in connect_list[i]:

            out_list[j] -= 1

            if dp_list[j] < dp_list[i] + 1:

                dp_list[j] = dp_list[i] + 1

            if out_list[j] == 0:

                new_search_list.append(j)

    search_list = new_search_list



print((max(dp_list)))
n, m = [int(v) for v in input().split()]
edge = []
in_list = [0] * n
out_list = [0] * n
dp_list = [0] * n
connect_list = [[] for i in range(n)]
for i in range(m):
    s, t = [int(v) - 1 for v in input().split()]
    edge.append((s, t))
    in_list[t] += 1
    out_list[s] += 1
    connect_list[t].append(s)
search_list = [i for i, v in enumerate(out_list) if v == 0]
while search_list:
    new_search_list = []
    for i in search_list:
        for j in connect_list[i]:
            out_list[j] -= 1
            if dp_list[j] < dp_list[i] + 1:
                dp_list[j] = dp_list[i] + 1
            if out_list[j] == 0:
                new_search_list.append(j)
    search_list = new_search_list
print((max(dp_list)))

import bisect, collections, copy, heapq, itertools, math, sys

input = sys.stdin.readline
n, m = [int(v) for v in input().split()]
edge = []
in_list = [0] * n
out_list = [0] * n
dp_list = [0] * n
connect_list = [[] for i in range(n)]
for i in range(m):
    s, t = [int(v) - 1 for v in input().split()]
    edge.append((s, t))
    in_list[t] += 1
    out_list[s] += 1
    connect_list[t].append(s)
search_list = [i for i, v in enumerate(out_list) if v == 0]
while search_list:
    new_search_list = []
    for i in search_list:
        for j in connect_list[i]:
            out_list[j] -= 1
            if dp_list[j] < dp_list[i] + 1:
                dp_list[j] = dp_list[i] + 1
            if out_list[j] == 0:
                new_search_list.append(j)
    search_list = new_search_list
print((max(dp_list)))

+import bisect, collections, copy, heapq, itertools, math, sys + +input = sys.stdin.readline