Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp)
day = 0
game = [0]*N
while rem:
    day += 1
    impossible = True
    played = {i: False for i in range(N) if game[i] < N-1}
    for i in played.keys():
        if played[i]: continue
        opposite = A[i][game[i]] - 1
        if not played[opposite] and i+1 == A[opposite][game[opposite]]:
            impossible = False
            played[i] = True; played[opposite] = True;
            rem -= 1
            game[i] += 1; game[opposite] += 1
    if impossible: print(-1); break
else:
    print(day)

Fixed submission:
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp[::-1])
day = 0
next_opponent = [-1]*N
played_yesterday = list(range(N))
while rem:
    day += 1
    impossible = True
    for p in played_yesterday:
        if A[p]: next_opponent[p] = A[p].pop() - 1
    played_today = []
    for p in played_yesterday:
        candidate = next_opponent[p]
        if candidate == -1: continue # player p had already finished all the game
        elif next_opponent[candidate] == p: # matched
            impossible = False
            rem -= 1
            played_today += [p, candidate]
            next_opponent[p], next_opponent[candidate] = -1, -1
    if impossible: print(-1); break
    played_yesterday = played_today
else:
    print(day)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp)
day = 0
game = [0]*N
while rem:
    day += 1
    impossible = True
    played = [False]*N
    for i in range(N):
        if played[i] or game[i] >= N-1: continue
        opposite = A[i][game[i]] - 1
        if not played[opposite] and i+1 == A[opposite][game[opposite]]:
            impossible = False
            played[i] = True; played[opposite] = True;
            rem -= 1
            game[i] += 1; game[opposite] += 1
    if impossible: print(-1); break
else:
    print(day)

Fixed submission:
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp[::-1])
day = 0
next_opponent = [-1]*N
played_yesterday = list(range(N))
while rem:
    day += 1
    impossible = True
    for p in played_yesterday:
        if A[p]: next_opponent[p] = A[p].pop() - 1
    played_today = []
    for p in played_yesterday:
        candidate = next_opponent[p]
        if candidate == -1: continue # player p had already finished all the game
        elif next_opponent[candidate] == p: # matched
            impossible = False
            rem -= 1
            played_today += [p, candidate]
            next_opponent[p], next_opponent[candidate] = -1, -1
    if impossible: print(-1); break
    played_yesterday = played_today
else:
    print(day)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp)
day = 0
game = [0]*N
while rem:
    day += 1
    impossible = True
    played = [False]*N
    for i in range(N):
        if played[i] or game[i] >= N-1: continue
        opposite = A[i][game[i]] - 1
        if not played[opposite] and i+1 == A[opposite][game[opposite]]:
            impossible = False
            played[i] = True; played[opposite] = True;
            rem -= 1
            game[i] += 1; game[opposite] += 1
    if impossible: print(-1); break
else:
    print(day)

Fixed submission:
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp[::-1])
day = 0
next_opponent = [-1]*N
played_yesterday = list(range(N))
while rem:
    day += 1
    impossible = True
    for p in played_yesterday:
        if A[p]: next_opponent[p] = A[p].pop() - 1
    played_today = []
    for p in played_yesterday:
        candidate = next_opponent[p]
        if candidate == -1: continue # player p had already finished all the game
        elif next_opponent[candidate] == p: # matched
            impossible = False
            rem -= 1
            played_today += [p, candidate]
            next_opponent[p], next_opponent[candidate] = -1, -1
    if impossible: print(-1); break
    played_yesterday = played_today
else:
    print(day)