Buggy python submission (verdict: Time Limit Exceeded):
from sys import setrecursionlimit, stdin


def f(i, links, dp):
    if dp[i] != -1:
        return dp[i]
    if len(links[i]) == 0:
        dp[i] = 0
    else:
        dp[i] = max(f(j, links, dp) for j in links[i]) + 1
    return dp[i]


def main():
    setrecursionlimit(10 ** 6)
    readline = stdin.readline

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

    links = [[] for _ in range(N)]
    for _ in range(M):
        x, y = map(int, readline().split())
        links[x - 1].append(y - 1)

    dp = [-1] * N
    print(max(f(i, links, dp) for i in range(N)))


main()


Fixed submission:
from sys import setrecursionlimit


def f(i, links, dp):
  if dp[i] != -1:
    return dp[i]
  if len(links[i]) == 0:
    dp[i] = 0
  else:
    dp[i] = max(f(j, links, dp) for j in links[i]) + 1
  return dp[i]


setrecursionlimit(1000000)

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

links = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(int, input().split())
  links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))

Buggy python submission (verdict: Time Limit Exceeded):
from sys import setrecursionlimit, stdin


def f(i, links, dp):
    if dp[i] != -1:
        return dp[i]
    if len(links[i]) == 0:
        dp[i] = 0
    else:
        dp[i] = max(f(j, links, dp) for j in links[i]) + 1
    return dp[i]


setrecursionlimit(10 ** 6)
readline = stdin.readline

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

links = [[] for _ in range(N)]
for _ in range(M):
    x, y = map(int, readline().split())
    links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))


Fixed submission:
from sys import setrecursionlimit


def f(i, links, dp):
  if dp[i] != -1:
    return dp[i]
  if len(links[i]) == 0:
    dp[i] = 0
  else:
    dp[i] = max(f(j, links, dp) for j in links[i]) + 1
  return dp[i]


setrecursionlimit(1000000)

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

links = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(int, input().split())
  links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))

Buggy python submission (verdict: Time Limit Exceeded):
from sys import setrecursionlimit


def f(i, links, dp):
  if dp[i] != -1:
    return dp[i]
  if len(links[i]) == 0:
    dp[i] = 0
  else:
    dp[i] = max(f(j, links, dp) for j in links[i]) + 1
  return dp[i]


setrecursionlimit(1000000)

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

links = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(int, input().split())
  links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))


Fixed submission:
from sys import setrecursionlimit


def f(i, links, dp):
  if dp[i] != -1:
    return dp[i]
  if len(links[i]) == 0:
    dp[i] = 0
  else:
    dp[i] = max(f(j, links, dp) for j in links[i]) + 1
  return dp[i]


setrecursionlimit(1000000)

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

links = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(int, input().split())
  links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))

Buggy python submission (verdict: Time Limit Exceeded):
from sys import setrecursionlimit


def f(i, links, dp):
    if dp[i] != -1:
        return dp[i]
    if len(links[i]) == 0:
        dp[i] = 0
    else:
        dp[i] = max(f(j, links, dp) for j in links[i]) + 1
    return dp[i]


setrecursionlimit(10 ** 6)

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

links = [[] for _ in range(N)]
for _ in range(M):
    x, y = map(int, input().split())
    links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))


Fixed submission:
from sys import setrecursionlimit


def f(i, links, dp):
  if dp[i] != -1:
    return dp[i]
  if len(links[i]) == 0:
    dp[i] = 0
  else:
    dp[i] = max(f(j, links, dp) for j in links[i]) + 1
  return dp[i]


setrecursionlimit(1000000)

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

links = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(int, input().split())
  links[x - 1].append(y - 1)

dp = [-1] * N
print(max(f(i, links, dp) for i in range(N)))