Buggy python submission (verdict: Time Limit Exceeded):
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

MAXN = 1005
MAXV = MAXN * (MAXN - 1)//2
G = [[] for _ in range(MAXV)]

id = [[-1] * MAXN for _ in range(MAXN)]
def toId(i,j):
    if i > j:
        i,j = j,i
    return id[i][j]


finished = [0] * MAXV
dp = [-1] * MAXV
def dfs(v):
    if dp[v] != -1:
        if finished[v] == 0:
            return -1
        return dp[v]
    dp[v] = 0
    for e in G[v]:
        res = dfs(e)
        if res == -1:
            return -1
        dp[v] = max(dp[v],res + 1)
    finished[v] = 1
    return dp[v]


def main():
    n = int(input())
    a = [[int(i) - 1 for i in input().split()] for _ in range(n)]

    v = 0
    for i in range(n):
        for j in range(i + 1,n):
            id[i][j] = v
            v += 1
    
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i,a[i][j])
        for j in range(n - 2):
            G[a[i][j]].append(a[i][j + 1])
    
    ans = 0
    for i in range(v):
        res = dfs(i)
        if res == -1:
            ans = -1
            break
        ans = max(ans,dfs(i) + 1)
    
    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

MAXN = 1005
MAXV = MAXN * (MAXN - 1)//2
G = [[] for _ in range(MAXV)]

id = [[-1] * MAXN for _ in range(MAXN)]
def toId(i,j):
    if i > j:
        i,j = j,i
    return id[i][j]


finished = [0] * MAXV
dp = [-1] * MAXV
def dfs(v):
    if dp[v] != -1:
        if finished[v] == 0:
            return -1
        return dp[v]
    dp[v] = 0
    for e in G[v]:
        res = dfs(e)
        if res == -1:
            return -1
        dp[v] = max(dp[v],res + 1)
    finished[v] = 1
    return dp[v]


def main():
    n = int(input())
    a = [[int(i) - 1 for i in input().split()] for _ in range(n)]

    v = 0
    for i in range(n):
        for j in range(i + 1,n):
            id[i][j] = v
            v += 1
    
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i,a[i][j])
        for j in range(n - 2):
            G[a[i][j]].append(a[i][j + 1])
    
    ans = 0
    for i in range(v):
        res = dfs(i)
        if res == -1:
            ans = -1
            break
        ans = max(ans,dfs(i) + 1)
    
    print(ans)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

def main():
    n = int(input())
    a = []
    for _ in range(n):
        b = [int(i) - 1 for i in input().split()]
        a.append(b[::-1])
    
    q = set()

    def check(i):
        if len(a[i]) == 0:
            return
        j = a[i][-1]
        if a[j][-1] == i:
            if i > j:
                i,j = j,i
            q.add((i,j))
    
    for i in range(n):
        check(i)
        
    day = 0
    while len(q):
        day += 1
        prevQ = deepcopy(q)
        q.clear()
        for i,j in prevQ:
            a[i].pop()
            a[j].pop()
        for i,j in prevQ:
            check(i)
            check(j)
    
    ans = day
    for i in range(n):
        if len(a[i]) != 0:
            ans = -1
    
    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

MAXN = 1005
MAXV = MAXN * (MAXN - 1)//2
G = [[] for _ in range(MAXV)]

id = [[-1] * MAXN for _ in range(MAXN)]
def toId(i,j):
    if i > j:
        i,j = j,i
    return id[i][j]


finished = [0] * MAXV
dp = [-1] * MAXV
def dfs(v):
    if dp[v] != -1:
        if finished[v] == 0:
            return -1
        return dp[v]
    dp[v] = 0
    for e in G[v]:
        res = dfs(e)
        if res == -1:
            return -1
        dp[v] = max(dp[v],res + 1)
    finished[v] = 1
    return dp[v]


def main():
    n = int(input())
    a = [[int(i) - 1 for i in input().split()] for _ in range(n)]

    v = 0
    for i in range(n):
        for j in range(i + 1,n):
            id[i][j] = v
            v += 1
    
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i,a[i][j])
        for j in range(n - 2):
            G[a[i][j]].append(a[i][j + 1])
    
    ans = 0
    for i in range(v):
        res = dfs(i)
        if res == -1:
            ans = -1
            break
        ans = max(ans,dfs(i) + 1)
    
    print(ans)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

def main():
    n = int(input())
    a = []
    for _ in range(n):
        b = [int(i) - 1 for i in input().split()]
        a.append(b[::-1])
    
    q = set()

    def check(i):
        if len(a[i]) == 0:
            return
        j = a[i][-1]
        if a[j][-1] == i:
            if i > j:
                i,j = j,i
            q.add((i,j))
    
    for i in range(n):
        check(i)
        
    day = 0
    while len(q):
        day += 1
        prevQ = deepcopy(q)
        q.clear()
        for i,j in prevQ:
            a[i].pop()
            a[j].pop()
        for i,j in prevQ:
            check(i)
            check(j)
    
    ans = day
    for i in range(n):
        if len(a[i]) != 0:
            ans = -1
    
    print(ans)

if __name__=='__main__':
    main()

Fixed submission:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

MAXN = 1005
MAXV = MAXN * (MAXN - 1)//2
G = [[] for _ in range(MAXV)]

id = [[-1] * MAXN for _ in range(MAXN)]
def toId(i,j):
    if i > j:
        i,j = j,i
    return id[i][j]


finished = [0] * MAXV
dp = [-1] * MAXV
def dfs(v):
    if dp[v] != -1:
        if finished[v] == 0:
            return -1
        return dp[v]
    dp[v] = 0
    for e in G[v]:
        res = dfs(e)
        if res == -1:
            return -1
        dp[v] = max(dp[v],res + 1)
    finished[v] = 1
    return dp[v]


def main():
    n = int(input())
    a = [[int(i) - 1 for i in input().split()] for _ in range(n)]

    v = 0
    for i in range(n):
        for j in range(i + 1,n):
            id[i][j] = v
            v += 1
    
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i,a[i][j])
        for j in range(n - 2):
            G[a[i][j]].append(a[i][j + 1])
    
    ans = 0
    for i in range(v):
        res = dfs(i)
        if res == -1:
            ans = -1
            break
        ans = max(ans,dfs(i) + 1)
    
    print(ans)

if __name__=='__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)

n = int(input())
pair = []
for i in range(n):
    for j in range(i + 1,n):
        pair.append((i,j))

G = {i:set() for i in pair}
inedge = {i:0 for i in pair}
for i in range(n):
    a = [int(i) - 1 for i in input().split()]
    for j in range(n - 2):
        if i <= a[j] and i <= a[j + 1]:
            G[(i,a[j])].add((i,a[j + 1]))
            inedge[(i,a[j + 1])] += 1
        elif i <= a[j] and i > a[j + 1]:
            G[(i,a[j])].add((a[j + 1],i))
            inedge[(a[j + 1],i)] += 1
        elif i > a[j] and i <= a[j + 1]:
            G[(a[j],i)].add((i,a[j + 1]))
            inedge[(i,a[j + 1])] += 1
        else:
            G[(a[j],i)].add((a[j + 1],i))
            inedge[(a[j + 1],i)] += 1
    
visited = {i:0 for i in pair}
finished = {i:0 for i in pair}
print(G)

flag = 1
def dfs(node): #サイクルの検出用
    global flag
    visited[i] = 1
    for e in G[node]:
        if finished[node]:
            continue
        if visited[node] and finished[node] == 0:
            flag = 0
            return
        dfs(e)
    if flag == 0:
        return
    finished[node] = 1

dfs((0,1))
    
if flag == 1:
    st = deque()
    dist = {i:-1 for i in pair}
    for i in pair:
        if inedge[i] == 0:
            st.append(i)
            dist[i] = 1
        
    while len(st):
        node = st.pop()
        for e in G[node]:
            dist[e] = max(dist[e],dist[node] + 1)
            inedge[e] -= 1
            if inedge[e] == 0:
                st.append(e)  
    print(dist) 
    print(max(dist.values()))
else:
    print(-1)



Fixed submission:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
from copy import deepcopy 

MAXN = 1005
MAXV = MAXN * (MAXN - 1)//2
G = [[] for _ in range(MAXV)]

id = [[-1] * MAXN for _ in range(MAXN)]
def toId(i,j):
    if i > j:
        i,j = j,i
    return id[i][j]


finished = [0] * MAXV
dp = [-1] * MAXV
def dfs(v):
    if dp[v] != -1:
        if finished[v] == 0:
            return -1
        return dp[v]
    dp[v] = 0
    for e in G[v]:
        res = dfs(e)
        if res == -1:
            return -1
        dp[v] = max(dp[v],res + 1)
    finished[v] = 1
    return dp[v]


def main():
    n = int(input())
    a = [[int(i) - 1 for i in input().split()] for _ in range(n)]

    v = 0
    for i in range(n):
        for j in range(i + 1,n):
            id[i][j] = v
            v += 1
    
    for i in range(n):
        for j in range(n - 1):
            a[i][j] = toId(i,a[i][j])
        for j in range(n - 2):
            G[a[i][j]].append(a[i][j + 1])
    
    ans = 0
    for i in range(v):
        res = dfs(i)
        if res == -1:
            ans = -1
            break
        ans = max(ans,dfs(i) + 1)
    
    print(ans)

if __name__=='__main__':
    main()