Buggy python submission (verdict: Runtime Error):
from _collections import defaultdict
import sys


class Graph:

    def __init__(self, n):
        self.g = defaultdict(list)
        self.dp = [-1 for i in range(n + 1)]

    def add_edge(self, x, y):
        self.g[x].append(y)

    def longest_path_util(self, node):

        if self.dp[node] != -1:
            return self.dp[node]

        leaf = 1
        best_child = 0
        for neib in self.g[node]:
                leaf = 0
                best_child = max(best_child, self.longest_path_util(neib))

        if leaf != 0:

            self.dp[node] = 0

        else:
            self.dp[node] = best_child + 1

        return self.dp[node]

    def longest_path(self, n):

        ans = 0

        for i in range(1, n + 1):

            ans = max(ans, self.longest_path_util(i))

        return ans


n, k =  map(int, input().split())
graph = Graph(n)
for _ in range(k):
    x, y = list(map(int, sys.stdin.readline().split()))
    graph.add_edge(x, y)

print(graph.longest_path(n))






Fixed submission:
from _collections import defaultdict
import sys
import collections
import sys
sys.setrecursionlimit(10**6)


def get_ls():
    return list(map(int, input().split()))


def longest_path_util(node, graph, dp):

    res = 0

    if dp[node] != -1:
        return dp[node]

    for neib in graph[node]:
        res = max(res, longest_path_util(neib, graph, dp) + 1)

    dp[node] = res

    return dp[node]


graph = defaultdict(list)

[n, k] = get_ls()

dp = [-1] * n

for _ in range(k):
    [x, y] = get_ls()
    graph[x - 1].append(y - 1)

ans = 0

for i in range(n):
    ans = max(ans, longest_path_util(i, graph, dp))

print(ans)

Buggy python submission (verdict: Runtime Error):
from _collections import defaultdict
import sys

class Graph:

    def __init__(self, n):
        self.g = defaultdict(list)
        self.dp = [-1 for i in range(n + 1)]


    def add_edge(self, x, y):
        self.g[x].append(y)

    def longest_path_util(self, node):

        # if node not in self.g:
        #     # print(node)
        #     return 0

        if self.dp[node] != -1:
            return self.dp[node]

        leaf = 1
        best_child = 0
        for neib in self.g[node]:
                leaf = 0
                best_child = max(best_child, self.longest_path_util(neib))

        if leaf != 0:

            self.dp[node] = 0

        else:
            self.dp[node] = best_child + 1

        return self.dp[node]


    def longest_path(self, n):

        ans = 0

        for i in range(1, n + 1):

            ans = max(ans, self.longest_path_util(i))


        return ans


n, k =  list(map(int, sys.stdin.readline().split()))
graph = Graph(n)
for _ in range(k):
    x, y = list(map(int, sys.stdin.readline().split()))
    graph.add_edge(x, y)

# print(graph.g)
print(graph.longest_path(n))






Fixed submission:
from _collections import defaultdict
import sys
import collections
import sys
sys.setrecursionlimit(10**6)


def get_ls():
    return list(map(int, input().split()))


def longest_path_util(node, graph, dp):

    res = 0

    if dp[node] != -1:
        return dp[node]

    for neib in graph[node]:
        res = max(res, longest_path_util(neib, graph, dp) + 1)

    dp[node] = res

    return dp[node]


graph = defaultdict(list)

[n, k] = get_ls()

dp = [-1] * n

for _ in range(k):
    [x, y] = get_ls()
    graph[x - 1].append(y - 1)

ans = 0

for i in range(n):
    ans = max(ans, longest_path_util(i, graph, dp))

print(ans)

Buggy python submission (verdict: Runtime Error):
from _collections import defaultdict
import sys

class Graph:

    def __init__(self, n):
        self.g = defaultdict(list)
        self.dp = [-1 for i in range(n + 1)]


    def add_edge(self, x, y):
        self.g[x].append(y)

    def longest_path_util(self, node):

        # if node not in self.g:
        #     # print(node)
        #     return 0

        if self.dp[node] != -1:
            return self.dp[node]

        leaf = 1
        best_child = 0
        for neib in self.g[node]:
                leaf = 0
                best_child = max(best_child, self.longest_path_util(neib))

        if leaf != 0:

            self.dp[node] = 0

        else:
            self.dp[node] = best_child + 1

        return self.dp[node]


    def longest_path(self, n):

        for i in range(1, n + 1):

            if self.dp[i] == -1:
                self.longest_path_util(i)

        return max(self.dp)


n, k =  list(map(int, sys.stdin.readline().split()))
graph = Graph(n)
for _ in range(k):
    x, y = list(map(int, sys.stdin.readline().split()))
    graph.add_edge(x, y)

# print(graph.g)
print(graph.longest_path(n))






Fixed submission:
from _collections import defaultdict
import sys
import collections
import sys
sys.setrecursionlimit(10**6)


def get_ls():
    return list(map(int, input().split()))


def longest_path_util(node, graph, dp):

    res = 0

    if dp[node] != -1:
        return dp[node]

    for neib in graph[node]:
        res = max(res, longest_path_util(neib, graph, dp) + 1)

    dp[node] = res

    return dp[node]


graph = defaultdict(list)

[n, k] = get_ls()

dp = [-1] * n

for _ in range(k):
    [x, y] = get_ls()
    graph[x - 1].append(y - 1)

ans = 0

for i in range(n):
    ans = max(ans, longest_path_util(i, graph, dp))

print(ans)