Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    rem = N * (N - 1) // 2
    for day in range(N * (N - 1) // 2):
        did_play = [0] * N
        for i in range(N):
            if did_play[i] or A[i] == []:
                continue
            opponent = A[i][-1]
            if A[opponent][-1] == i and not did_play[opponent]:
                A[i].pop()
                A[opponent].pop()
                rem -= 1
                did_play[i] = 1
                did_play[opponent] = 1
        if sum(did_play) == 0:
            break
        if rem == 0:
            return day + 1
    return -1


N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split()))[::-1] for _ in range(N)]
print(solve())


Fixed submission:
from sys import setrecursionlimit


def encode(i, j):
    if i < j:
        i, j = j, i
    return i * (i - 1) // 2 + j + 1


def dfs(v):
    if visited[v] == 2:
        return dp[v]
    visited[v] = 1
    for nv in g[v]:
        if visited[nv] == 1:
            print(-1)
            exit()
        dp[v] = max(dp[v], dfs(nv) + 1)
    visited[v] = 2
    return dp[v]


setrecursionlimit(10 ** 6)
N = int(input())
GAMES_CNT = N * (N - 1) // 2
g = [[] for _ in range(GAMES_CNT + 1)]
for i in range(N):
    orig = 0
    for j in map(lambda x: int(x) - 1, input().split()):
        dest = encode(i, j)
        g[orig].append(dest)
        orig = dest
dp = [0] * (GAMES_CNT + 1)
visited = [0] * (GAMES_CNT + 1)
print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    rem = N * (N - 1) // 2
    for day in range(N * (N - 1) // 2):
        did_play = [0] * N
        for i in range(N):
            if did_play[i] or A[i] == []:
                continue
            opponent = A[i][-1]
            if A[opponent][-1] == i and not did_play[opponent]:
                A[i].pop()
                A[opponent].pop()
                rem -= 1
                did_play[i] = 1
                did_play[opponent] = 1
        if sum(did_play) == 0:
            break
        if rem == 0:
            return day + 1
    return -1


N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split()))[::-1] for _ in range(N)]
print(solve())


Fixed submission:
from sys import setrecursionlimit


def encode(i, j):
    if i < j:
        i, j = j, i
    return i * (i - 1) // 2 + j + 1


def dfs(v):
    if visited[v] == 2:
        return dp[v]
    visited[v] = 1
    for nv in g[v]:
        if visited[nv] == 1:
            print(-1)
            exit()
        dp[v] = max(dp[v], dfs(nv) + 1)
    visited[v] = 2
    return dp[v]


setrecursionlimit(10 ** 6)
N = int(input())
GAMES_CNT = N * (N - 1) // 2
g = [[] for _ in range(GAMES_CNT + 1)]
for i in range(N):
    orig = 0
    for j in map(lambda x: int(x) - 1, input().split()):
        dest = encode(i, j)
        g[orig].append(dest)
        orig = dest
dp = [0] * (GAMES_CNT + 1)
visited = [0] * (GAMES_CNT + 1)
print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split()))[::-1] for _ in range(N)]

for day in range(N * (N - 1) // 2):
    did_play = [0] * N
    for i in range(N):
        if did_play[i] or A[i] == []:
            continue
        opponent = A[i][-1]
        if A[opponent][-1] == i and not did_play[opponent]:
            A[i].pop()
            A[opponent].pop()
            did_play[i] = 1
            did_play[opponent] = 1
    if all(a == [] for a in A):
        print(day + 1)
        break
else:
    print(-1)


Fixed submission:
from sys import setrecursionlimit


def encode(i, j):
    if i < j:
        i, j = j, i
    return i * (i - 1) // 2 + j + 1


def dfs(v):
    if visited[v] == 2:
        return dp[v]
    visited[v] = 1
    for nv in g[v]:
        if visited[nv] == 1:
            print(-1)
            exit()
        dp[v] = max(dp[v], dfs(nv) + 1)
    visited[v] = 2
    return dp[v]


setrecursionlimit(10 ** 6)
N = int(input())
GAMES_CNT = N * (N - 1) // 2
g = [[] for _ in range(GAMES_CNT + 1)]
for i in range(N):
    orig = 0
    for j in map(lambda x: int(x) - 1, input().split()):
        dest = encode(i, j)
        g[orig].append(dest)
        orig = dest
dp = [0] * (GAMES_CNT + 1)
visited = [0] * (GAMES_CNT + 1)
print(dfs(0))