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

num_game_left = N * (N - 1) // 2
num_days = 0
left_players = set(range(N))
while 0 < num_game_left:
    game_players = set()
    for i in left_players:
        if i in game_players:
            continue
        j = A[i][-1]
        if A[j][-1] == i:
            game_players.add(i)
            game_players.add(j)
    num_game_left -= len(game_players) // 2

    if not game_players:
        break

    num_days += 1
    for i in game_players:
        A[i].pop()
        if not A[i]:
            left_players.remove(i)

if num_game_left == 0:
    print(num_days)
else:
    print(-1)

Fixed submission:
N = int(input())
A = []
for _ in range(N):
    A.append([x - 1 for x in map(int, input().split())])

game = []
for p1 in range(N):
    p2 = A[p1][0]
    if A[p2][0] == p1 and p1 < p2:
        game.append((p1, p2))

cursors = [0] * N
days = 0
while game:
    days += 1
    new_game = []
    for p1, p2 in game:
        cursors[p1] += 1
        if cursors[p1] < N - 1:
            p3 = A[p1][cursors[p1]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p1:
                new_game.append((p1, p3))
        cursors[p2] += 1
        if cursors[p2] < N - 1:
            p3 = A[p2][cursors[p2]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p2:
                new_game.append((p2, p3))
    game = new_game

if all(c == N - 1 for c in cursors):
    print(days)
else:
    print(-1)

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

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

num_game_left = N * (N - 1) // 2
num_days = 0
left_players = set(range(N))
while 0 < num_game_left:
    game_players = set()
    for i in left_players:
        if i in game_players:
            continue
        j = A[i][0]
        if A[j] and A[j][0] == i:
            game_players.add(i)
            game_players.add(j)
    num_game_left -= len(game_players) // 2

    if not game_players:
        break

    num_days += 1
    for i in game_players:
        A[i].popleft()
        if not A[i]:
            left_players.remove(i)

if num_game_left == 0:
    print(num_days)
else:
    print(-1)

Fixed submission:
N = int(input())
A = []
for _ in range(N):
    A.append([x - 1 for x in map(int, input().split())])

game = []
for p1 in range(N):
    p2 = A[p1][0]
    if A[p2][0] == p1 and p1 < p2:
        game.append((p1, p2))

cursors = [0] * N
days = 0
while game:
    days += 1
    new_game = []
    for p1, p2 in game:
        cursors[p1] += 1
        if cursors[p1] < N - 1:
            p3 = A[p1][cursors[p1]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p1:
                new_game.append((p1, p3))
        cursors[p2] += 1
        if cursors[p2] < N - 1:
            p3 = A[p2][cursors[p2]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p2:
                new_game.append((p2, p3))
    game = new_game

if all(c == N - 1 for c in cursors):
    print(days)
else:
    print(-1)

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

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

num_game_left = N * (N - 1) // 2
num_days = 0
left_players = set(range(N))
while 0 < num_game_left:
    game_players = set()
    for i in left_players:
        if i in game_players:
            continue
        j = A[i][0]
        if A[j] and A[j][0] == i:
            game_players.add(i)
            game_players.add(j)
    num_game_left -= len(game_players) // 2

    if len(game_players) == 0:
        break

    num_days += 1
    for i in game_players:
        A[i].popleft()
        if not A[i]:
            left_players.remove(i)

if num_game_left == 0:
    print(num_days)
else:
    print(-1)

Fixed submission:
N = int(input())
A = []
for _ in range(N):
    A.append([x - 1 for x in map(int, input().split())])

game = []
for p1 in range(N):
    p2 = A[p1][0]
    if A[p2][0] == p1 and p1 < p2:
        game.append((p1, p2))

cursors = [0] * N
days = 0
while game:
    days += 1
    new_game = []
    for p1, p2 in game:
        cursors[p1] += 1
        if cursors[p1] < N - 1:
            p3 = A[p1][cursors[p1]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p1:
                new_game.append((p1, p3))
        cursors[p2] += 1
        if cursors[p2] < N - 1:
            p3 = A[p2][cursors[p2]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p2:
                new_game.append((p2, p3))
    game = new_game

if all(c == N - 1 for c in cursors):
    print(days)
else:
    print(-1)

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

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

num_game_left = N * (N - 1) // 2
num_days = 0
while 0 < num_game_left:
    ids = [None] * N
    for i in range(N):
        if A[i]:
            ids[i] = A[i][0]
        else:
            ids[i] = None

    game_players = set()
    for i in range(N):
        if ids[i] is None:
            continue
        j = ids[i]
        if ids[j] == i:
            game_players.add(i)
            game_players.add(j)
    assert len(game_players) % 2 == 0
    num_game_left -= len(game_players) // 2

    if len(game_players) == 0:
        break

    num_days += 1
    for i in game_players:
        A[i].popleft()

if num_game_left == 0:
    print(num_days)
else:
    print(-1)

Fixed submission:
N = int(input())
A = []
for _ in range(N):
    A.append([x - 1 for x in map(int, input().split())])

game = []
for p1 in range(N):
    p2 = A[p1][0]
    if A[p2][0] == p1 and p1 < p2:
        game.append((p1, p2))

cursors = [0] * N
days = 0
while game:
    days += 1
    new_game = []
    for p1, p2 in game:
        cursors[p1] += 1
        if cursors[p1] < N - 1:
            p3 = A[p1][cursors[p1]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p1:
                new_game.append((p1, p3))
        cursors[p2] += 1
        if cursors[p2] < N - 1:
            p3 = A[p2][cursors[p2]]
            if cursors[p3] < N - 1 and A[p3][cursors[p3]] == p2:
                new_game.append((p2, p3))
    game = new_game

if all(c == N - 1 for c in cursors):
    print(days)
else:
    print(-1)