Buggy python submission (verdict: Runtime Error):
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n-1):
        a[i][j] -= 1

MAXN = 1005
MAXV = MAXN*(MAXN-1)//2

to = [[] for _ in range(MAXV)]
# 頂点番号
id = [[0 for _ in range(MAXN)] for _ in range(MAXN)]
def toID(i, j):
    if i > j:
        i, j = j, i
    return id[i][j]

# 頂点番号のラベリング
V = 0
for i in range(n):
    for j in range(n):
        if i < j:
            id[i][j] += V
            V += 1

# aを、頂点番号に書き換え、試合の依存関係を表す辺を張る(辺の向きは逆にする)
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):
        to[a[i][j+1]].append(a[i][j])

# dfsする
visited = [False for _ in range(MAXV)]
calculated = [False for _ in range(MAXV)] # loop 検出用
dp = [0 for _ in range(MAXV)] # max length of path from v
def dfs(v):
    if visited[v]:
        if not calculated[v]:
            return -1
        return dp[v]
    visited[v] = True
    dp[v] = 1
    for u in to[v]:
        res = dfs(u)
        if res == -1:
            return -1
        dp[v] = max(dp[v], res+1)
    calculated[v] = True
    return dp[v]

ans = 0
ok = True
for i in range(V):
    res = dfs(i)
    if res == -1:
        ok = False
        break
    ans = max(ans, res)
if ok:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n-1):
        a[i][j] -= 1

MAXN = 1005
MAXV = MAXN*(MAXN-1)//2

to = [[] for _ in range(MAXV)]
# 頂点番号
id = [[0 for _ in range(MAXN)] for _ in range(MAXN)]
def toID(i, j):
    if i > j:
        i, j = j, i
    return id[i][j]

# 頂点番号のラベリング
V = 0
for i in range(n):
    for j in range(n):
        if i < j:
            id[i][j] += V
            V += 1

# aを、頂点番号に書き換え、試合の依存関係を表す辺を張る(辺の向きは逆にする)
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):
        to[a[i][j+1]].append(a[i][j])

# dfsする
visited = [False for _ in range(MAXV)]
calculated = [False for _ in range(MAXV)] # loop 検出用
dp = [0 for _ in range(MAXV)] # max length of path from v
def dfs(v):
    if visited[v]:
        if not calculated[v]:
            return -1
        return dp[v]
    visited[v] = True
    dp[v] = 1
    for u in to[v]:
        res = dfs(u)
        if res == -1:
            return -1
        dp[v] = max(dp[v], res+1)
    calculated[v] = True
    return dp[v]

ans = 0
ok = True
for i in range(V):
    res = dfs(i)
    if res == -1:
        ok = False
        break
    ans = max(ans, res)
if ok:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n = int(input())
a = [deque() for _ in range(n)]
cand = deque()
match = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
    inp = list(map(int, input().split()))
    for j in range(n-1):
        a[i].append(inp[j]-1)
    e = a[i].popleft()
    mi, mj = min(i, e), max(i, e)
    cand.append([mi, mj])
    match[mi][mj] += 1

ans = 0
flag = False
while len(cand) > 0:
    ans += 1
    flag = False
    q = deque()
    nextday = []
    while len(cand) > 0:
        mi, mj = cand.pop()
        if match[mi][mj] == 2:
            cand.remove([mi, mj])
            match[mi][mj] += 1
            flag = True
            for k in [mi, mj]:
                if len(a[k]) > 0:
                    e = a[k].popleft()
                    ni, nj = min(k, e), max(k, e)
                    nextday.append([ni, nj])
        else:
            q.append([mi, mj])
    for i in range(len(nextday)):
        q.append(nextday[i])
        match[nextday[i][0]][nextday[i][1]] += 1
    if not flag:
        break
    cand = q

if flag:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n-1):
        a[i][j] -= 1

MAXN = 1005
MAXV = MAXN*(MAXN-1)//2

to = [[] for _ in range(MAXV)]
# 頂点番号
id = [[0 for _ in range(MAXN)] for _ in range(MAXN)]
def toID(i, j):
    if i > j:
        i, j = j, i
    return id[i][j]

# 頂点番号のラベリング
V = 0
for i in range(n):
    for j in range(n):
        if i < j:
            id[i][j] += V
            V += 1

# aを、頂点番号に書き換え、試合の依存関係を表す辺を張る(辺の向きは逆にする)
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):
        to[a[i][j+1]].append(a[i][j])

# dfsする
visited = [False for _ in range(MAXV)]
calculated = [False for _ in range(MAXV)] # loop 検出用
dp = [0 for _ in range(MAXV)] # max length of path from v
def dfs(v):
    if visited[v]:
        if not calculated[v]:
            return -1
        return dp[v]
    visited[v] = True
    dp[v] = 1
    for u in to[v]:
        res = dfs(u)
        if res == -1:
            return -1
        dp[v] = max(dp[v], res+1)
    calculated[v] = True
    return dp[v]

ans = 0
ok = True
for i in range(V):
    res = dfs(i)
    if res == -1:
        ok = False
        break
    ans = max(ans, res)
if ok:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n = int(input())
a = [deque() for _ in range(n)]
cand = deque()
match = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
    inp = list(map(int, input().split()))
    for j in range(n-1):
        a[i].append(inp[j]-1)
    e = a[i].popleft()
    mi, mj = min(i, e), max(i, e)
    cand.append([mi, mj])
    match[mi][mj] += 1

ans = 0
flag = False
while len(cand) > 0:
    ans += 1
    flag = False
    q = deque()
    nextday = []
    while len(cand) > 0:
        mi, mj = cand.pop()
        if match[mi][mj] == 2:
            cand.remove([mi, mj])
            match[mi][mj] += 1
            flag = True
            for k in [mi, mj]:
                if len(a[k]) > 0:
                    e = a[k].popleft()
                    ni, nj = min(k, e), max(k, e)
                    nextday.append([ni, nj])
        else:
            q.append([mi, mj])
    for i in range(len(nextday)):
        q.append(nextday[i])
        match[nextday[i][0]][nextday[i][1]] += 1
    if not flag:
        break
    cand = q

if flag:
    print(ans)
else:
    print(-1)

Fixed submission:
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n-1):
        a[i][j] -= 1

MAXN = 1005
MAXV = MAXN*(MAXN-1)//2

to = [[] for _ in range(MAXV)]
# 頂点番号
id = [[0 for _ in range(MAXN)] for _ in range(MAXN)]
def toID(i, j):
    if i > j:
        i, j = j, i
    return id[i][j]

# 頂点番号のラベリング
V = 0
for i in range(n):
    for j in range(n):
        if i < j:
            id[i][j] += V
            V += 1

# aを、頂点番号に書き換え、試合の依存関係を表す辺を張る(辺の向きは逆にする)
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):
        to[a[i][j+1]].append(a[i][j])

# dfsする
visited = [False for _ in range(MAXV)]
calculated = [False for _ in range(MAXV)] # loop 検出用
dp = [0 for _ in range(MAXV)] # max length of path from v
def dfs(v):
    if visited[v]:
        if not calculated[v]:
            return -1
        return dp[v]
    visited[v] = True
    dp[v] = 1
    for u in to[v]:
        res = dfs(u)
        if res == -1:
            return -1
        dp[v] = max(dp[v], res+1)
    calculated[v] = True
    return dp[v]

ans = 0
ok = True
for i in range(V):
    res = dfs(i)
    if res == -1:
        ok = False
        break
    ans = max(ans, res)
if ok:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[list(map(int, input().split())) for _ in range(N)]
A.insert(0,[0]*(N-1))
day=0
fin=[0]*(N+1)
no_match=True
impossible=False
while sum(fin) < N:
  no_match=True
  fighter=[False]*(N+1)
  for i in range(1,N+1):
    if fin[i]==1:
      continue
    f=A[i][0]
    if fighter[i] or fighter[f]:
      continue
    if A[f][0]==i:
      no_match=False
      fighter[i]=True
      fighter[f]=True
      A[i]=A[i][1:]
      A[f]=A[f][1:]
      if len(A[i])==0:
        fin[i]=1
      if len(A[f])==0:
        fin[f]=1
  if no_match:
    impossible=True
    break
  day+=1
if impossible:
  print(-1)
else:
  print(day)
      

Fixed submission:
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
for i in range(n):
    for j in range(n-1):
        a[i][j] -= 1

MAXN = 1005
MAXV = MAXN*(MAXN-1)//2

to = [[] for _ in range(MAXV)]
# 頂点番号
id = [[0 for _ in range(MAXN)] for _ in range(MAXN)]
def toID(i, j):
    if i > j:
        i, j = j, i
    return id[i][j]

# 頂点番号のラベリング
V = 0
for i in range(n):
    for j in range(n):
        if i < j:
            id[i][j] += V
            V += 1

# aを、頂点番号に書き換え、試合の依存関係を表す辺を張る(辺の向きは逆にする)
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):
        to[a[i][j+1]].append(a[i][j])

# dfsする
visited = [False for _ in range(MAXV)]
calculated = [False for _ in range(MAXV)] # loop 検出用
dp = [0 for _ in range(MAXV)] # max length of path from v
def dfs(v):
    if visited[v]:
        if not calculated[v]:
            return -1
        return dp[v]
    visited[v] = True
    dp[v] = 1
    for u in to[v]:
        res = dfs(u)
        if res == -1:
            return -1
        dp[v] = max(dp[v], res+1)
    calculated[v] = True
    return dp[v]

ans = 0
ok = True
for i in range(V):
    res = dfs(i)
    if res == -1:
        ok = False
        break
    ans = max(ans, res)
if ok:
    print(ans)
else:
    print(-1)