Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
A = list(deque(map(lambda x: int(x) - 1, input().split())) for _ in range(N))
# print(A)
ans = 0
change = True
while True:
    change = False
    match = [-1] * N
    for player in range(N):
        if not A[player]:
            continue
        enemy = A[player][0]
        match[player] = enemy
        if match[enemy] == player:
            A[player].popleft()
            A[enemy].popleft()
            change = True
    if not change:
        break
    ans += 1


if all(len(row) == 0 for row in A):
    print(ans)
else:
    print('-1')

Fixed submission:
from collections import deque
from pprint import pprint
N = int(input())
nextGame = [deque()] + [deque(map(int , input().split())) for _ in range(N)]
#print(nextGame)

canPlay = [[False] * (N + 1) for _ in range(N + 1)]
#pprint(canPlay)
days = [0] * (N + 1)
que = ([i for i in range(N + 1)])

while que:
    now = que.pop()
    #print("------------------------")
    if not nextGame[now]:
        continue
    enemy = nextGame[now].popleft()
    canPlay[now][enemy] = True
    #print("now : ",now)
    #print("enemy : ",enemy)
    #pprint(canPlay)

    if canPlay[now][enemy] and canPlay[enemy][now]:
        days[now] = days[enemy] = max(days[now], days[enemy]) + 1
        que.append(now)
        que.append(enemy)
    #print("days : ",days)

for test in nextGame:
    if test:
        print("-1")
        exit(0)
print(max(days))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
A = list(deque(map(int, input().split())) for _ in range(N))
for i in range(N):
    A[i] = deque(map(lambda x: x - 1, A[i]))
# print(A)
ans = 0
change = True
while True:
    change = False
    work = [-1] * N
    for i in range(N):
        if not A[i]:
            continue
        enemy = A[i][0]
        work[i] = enemy
        if work[enemy] == i:
            A[i].popleft()
            A[enemy].popleft()
            change = True
    if not change:
        break
    ans += 1


empty = True
for row in A:
    if row:
        empty = False
if not empty:
    print('-1')
else:
    print(ans)



Fixed submission:
from collections import deque
from pprint import pprint
N = int(input())
nextGame = [deque()] + [deque(map(int , input().split())) for _ in range(N)]
#print(nextGame)

canPlay = [[False] * (N + 1) for _ in range(N + 1)]
#pprint(canPlay)
days = [0] * (N + 1)
que = ([i for i in range(N + 1)])

while que:
    now = que.pop()
    #print("------------------------")
    if not nextGame[now]:
        continue
    enemy = nextGame[now].popleft()
    canPlay[now][enemy] = True
    #print("now : ",now)
    #print("enemy : ",enemy)
    #pprint(canPlay)

    if canPlay[now][enemy] and canPlay[enemy][now]:
        days[now] = days[enemy] = max(days[now], days[enemy]) + 1
        que.append(now)
        que.append(enemy)
    #print("days : ",days)

for test in nextGame:
    if test:
        print("-1")
        exit(0)
print(max(days))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
A = list(deque(map(int, input().split())) for _ in range(N))
for i in range(N):
    A[i] = deque(map(lambda x: x - 1, A[i]))
# print(A)
ans = 0
change = True
while True:
    change = False
    work = [-1] * N
    for i in range(N):
        if not A[i]:
            continue
        enemy = A[i][0]
        work[i] = enemy
        if work[enemy] == i:
            A[i].popleft()
            A[enemy].popleft()
            change = True
    if not change:
        break
    ans += 1


empty = True
for row in A:
    if row:
        empty = False
if not empty:
    print('-1')
else:
    print(ans)



Fixed submission:
from collections import deque
from pprint import pprint
N = int(input())
nextGame = [deque()] + [deque(map(int , input().split())) for _ in range(N)]
#print(nextGame)

canPlay = [[False] * (N + 1) for _ in range(N + 1)]
#pprint(canPlay)
days = [0] * (N + 1)
que = ([i for i in range(N + 1)])

while que:
    now = que.pop()
    #print("------------------------")
    if not nextGame[now]:
        continue
    enemy = nextGame[now].popleft()
    canPlay[now][enemy] = True
    #print("now : ",now)
    #print("enemy : ",enemy)
    #pprint(canPlay)

    if canPlay[now][enemy] and canPlay[enemy][now]:
        days[now] = days[enemy] = max(days[now], days[enemy]) + 1
        que.append(now)
        que.append(enemy)
    #print("days : ",days)

for test in nextGame:
    if test:
        print("-1")
        exit(0)
print(max(days))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N = int(input())
A = list(deque(map(int, input().split())) for _ in range(N))
for i in range(N):
    for j in range(N - 1):
        A[i][j] -= 1

ans = 0
change = True
while change:
    change = False
    work = [-1] * N
    for i in range(N):
        if not A[i]:
            continue
        enemy = A[i][0]
        work[i] = enemy
        if work[work[i]] == i:
            A[i].popleft()
            A[enemy].popleft()
            change = True
    if not change:
        break
    ans += 1


empty = True
for row in A:
    if row:
        empty = False
if not empty:
    print('-1')
else:
    print(ans)



Fixed submission:
from collections import deque
from pprint import pprint
N = int(input())
nextGame = [deque()] + [deque(map(int , input().split())) for _ in range(N)]
#print(nextGame)

canPlay = [[False] * (N + 1) for _ in range(N + 1)]
#pprint(canPlay)
days = [0] * (N + 1)
que = ([i for i in range(N + 1)])

while que:
    now = que.pop()
    #print("------------------------")
    if not nextGame[now]:
        continue
    enemy = nextGame[now].popleft()
    canPlay[now][enemy] = True
    #print("now : ",now)
    #print("enemy : ",enemy)
    #pprint(canPlay)

    if canPlay[now][enemy] and canPlay[enemy][now]:
        days[now] = days[enemy] = max(days[now], days[enemy]) + 1
        que.append(now)
        que.append(enemy)
    #print("days : ",days)

for test in nextGame:
    if test:
        print("-1")
        exit(0)
print(max(days))