import sys

readline = sys.stdin.readline



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

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

indegree = [0 for i in range(N)]



for i in range(M):

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

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

  indegree[y - 1] += 1

  

stack = []

for i in range(len(indegree)):

  if indegree[i] == 0:

    stack.append(i)

    

cnt = -1

while stack:

  cnt += 1

  newstack = []

  while stack:

    v = stack.pop()

    for child in G[v]:

      indegree[child] -= 1

      if indegree[child] == 0:

        newstack.append(child)

  stack = newstack



print(cnt)
import sys

readline = sys.stdin.readline



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



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

indegree = [0] * N



for i in range(M):

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

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

  indegree[y - 1] += 1

  

start = set()

for i in range(len(indegree)):

  if indegree[i] == 0:

    start.add(i)

    

cnt = 0

while start:

  nex = set()

  hasnext = False

  for s in start:

    for y in G[s]:

      indegree[y] -= 1

      if indegree[y] == 0:

        nex.add(y)

        hasnext = True

  if hasnext:

    cnt += 1

  start = nex



print(cnt)  

  
import sys

readline = sys.stdin.readline
N, M = list(map(int, readline().split()))
G = [[] for i in range(N)]
indegree = [0 for i in range(N)]
for i in range(M):
    x, y = list(map(int, readline().split()))
    G[x - 1].append(y - 1)
    indegree[y - 1] += 1
stack = []
for i in range(len(indegree)):
    if indegree[i] == 0:
        stack.append(i)
cnt = -1
while stack:
    cnt += 1
    newstack = []
    while stack:
        v = stack.pop()
        for child in G[v]:
            indegree[child] -= 1
            if indegree[child] == 0:
                newstack.append(child)
    stack = newstack
print(cnt)

import sys

readline = sys.stdin.readline
N, M = list(map(int, readline().split()))
G = [[] for i in range(N)]
indegree = [0] * N
for i in range(M):
    x, y = list(map(int, readline().split()))
    G[x - 1].append(y - 1)
    indegree[y - 1] += 1
start = set()
for i in range(len(indegree)):
    if indegree[i] == 0:
        start.add(i)
cnt = 0
while start:
    nex = set()
    hasnext = False
    for s in start:
        for y in G[s]:
            indegree[y] -= 1
            if indegree[y] == 0:
                nex.add(y)
                hasnext = True
    if hasnext:
        cnt += 1
    start = nex
print(cnt)

-indegree = [0 for i in range(N)] +indegree = [0] * N -stack = [] +start = set() -        stack.append(i) -cnt = -1 -while stack: -    cnt += 1 -    newstack = [] -    while stack: -        v = stack.pop() -        for child in G[v]: -            indegree[child] -= 1 -            if indegree[child] == 0: -                newstack.append(child) -    stack = newstack +        start.add(i) +cnt = 0 +while start: +    nex = set() +    hasnext = False +    for s in start: +        for y in G[s]: +            indegree[y] -= 1 +            if indegree[y] == 0: +                nex.add(y) +                hasnext = True +    if hasnext: +        cnt += 1 +    start = nex