Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import islice
from collections import deque
read = map(int, sys.stdin.read().split())


def main():
    N = next(read)
    A = [deque()] + [deque(islice(read, N - 1)) for _ in range(N)]
    day = 0

    while True:
        done = set()
        for i in range(1, N + 1):
            if i not in done and A[i]:
                if A[A[i][0]][0] == i and A[i][0] not in done:
                    done.add(A[A[i][0]].popleft())
                    done.add(A[i].popleft())

        if len(done) == 0:
            for i in A:
                if len(i) > 0:
                    print(-1)
                    exit()
            else:
                print(day)
                exit()

        day += 1


if __name__ == '__main__':
    main()

Fixed submission:
import sys
from itertools import islice
from collections import deque
read = map(int, sys.stdin.read().split())

N = next(read)
A = [deque()] + [deque(islice(read, N - 1)) for _ in range(N)]
days = 1
available_players = deque(range(1, N + 1))
next_opponent = [0] * (N + 1)
played = set()

while available_players:
    player = available_players.popleft()
    if not A[player]:
        continue

    opponent = A[player].popleft()
    if next_opponent[opponent] == player:
        available_players.append(player)
        available_players.append(opponent)
        next_opponent[player] = 0
        next_opponent[opponent] = 0
        if player in played:
            days += 1
            played = set()
        played.add(player)
        played.add(opponent)
    else:
        next_opponent[player] = opponent

for i in A:
    if len(i) > 0:
        print(-1)
        exit()
else:
    print(days)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import islice
from collections import deque
read = map(int, sys.stdin.read().split())

N = next(read)
A = [deque()] + [deque(islice(read, N - 1)) for _ in range(N)]
day = 0

while True:
    done = set()
    for i in range(1, N + 1):
        if i not in done and A[i]:
            if A[A[i][0]][0] == i and A[i][0] not in done:
                done.add(A[A[i][0]].popleft())
                done.add(A[i].popleft())

    if len(done) == 0:
        for i in A:
            if len(i) > 0:
                print(-1)
                exit()
        else:
            print(day)
            exit()
    
    day += 1

Fixed submission:
import sys
from itertools import islice
from collections import deque
read = map(int, sys.stdin.read().split())

N = next(read)
A = [deque()] + [deque(islice(read, N - 1)) for _ in range(N)]
days = 1
available_players = deque(range(1, N + 1))
next_opponent = [0] * (N + 1)
played = set()

while available_players:
    player = available_players.popleft()
    if not A[player]:
        continue

    opponent = A[player].popleft()
    if next_opponent[opponent] == player:
        available_players.append(player)
        available_players.append(opponent)
        next_opponent[player] = 0
        next_opponent[opponent] = 0
        if player in played:
            days += 1
            played = set()
        played.add(player)
        played.add(opponent)
    else:
        next_opponent[player] = opponent

for i in A:
    if len(i) > 0:
        print(-1)
        exit()
else:
    print(days)