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

n = int(input())
a = [collections.deque(map(lambda s: int(s) - 1, input().split())) for _ in range(n)]

def f():
    day = 0
    players = set(range(n))

    while players:
        visited = set()
        removals = set()
        for i in players:
            if i in visited:
                continue
            
            x = a[i][0] # iの次候補
            if x in visited:
                continue

            y = a[x][0] # xの次候補
            if i == y:
                a[i].popleft()
                a[x].popleft()
                visited.add(i)
                visited.add(x)
                if not a[i]:
                    removals.add(i)
                if not a[x]:
                    removals.add(x)

        if visited:
            players -= removals
            day += 1
        else:
            return -1

    return day



print(f())

Fixed submission:
import collections

n = int(input())
a = [collections.deque(int(s) - 1 for s in input().split()) for _ in range(n)]

day = 0
players = set(range(n))
while any(v for v in a):
    nxt_players = set()
    for player in players:
        if len(a[player]) == 0 or player in nxt_players:
            continue
        
        other = a[player][0]
        if len(a[other]) == 0 or other in nxt_players:
            continue
        
        if player == a[other][0]:
            a[player].popleft()
            a[other].popleft()
            nxt_players.add(player)
            nxt_players.add(other)

    if nxt_players:
        day += 1
        players = nxt_players
    else:
        print(-1)
        exit()

print(day)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [list(map(lambda s: int(s) - 1, input().split())) for _ in range(n)]

def f():
    day = 0
    ptrs = [0] * n
    players = set(range(n))

    while players:
        visited = set()
        removals = set()
        for i in players:
            if i in visited:
                continue
            
            x = a[i][ptrs[i]] # iの次候補
            if x in visited:
                continue

            y = a[x][ptrs[x]] # xの次候補
            if i == y:
                ptrs[i] += 1
                ptrs[x] += 1
                visited.add(i)
                visited.add(x)
                if ptrs[i] == n - 1:
                    removals.add(i)
                if ptrs[x] == n - 1:
                    removals.add(x)

        # すべて未訪問の場合
        if len(visited) == 0:
            return -1
        players -= removals
        day += 1
    
    return day



print(f())

Fixed submission:
import collections

n = int(input())
a = [collections.deque(int(s) - 1 for s in input().split()) for _ in range(n)]

day = 0
players = set(range(n))
while any(v for v in a):
    nxt_players = set()
    for player in players:
        if len(a[player]) == 0 or player in nxt_players:
            continue
        
        other = a[player][0]
        if len(a[other]) == 0 or other in nxt_players:
            continue
        
        if player == a[other][0]:
            a[player].popleft()
            a[other].popleft()
            nxt_players.add(player)
            nxt_players.add(other)

    if nxt_players:
        day += 1
        players = nxt_players
    else:
        print(-1)
        exit()

print(day)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [list(map(lambda s: int(s) - 1, input().split())) for _ in range(n)]

def f():
    day = 0
    ptrs = [0] * n

    while True:
        if all(ptr == n - 1 for ptr in ptrs):
            return day

        visited = set()
        ok = False
        for i in range(n):
            if ptrs[i] == n - 1 or i in visited:
                continue
            
            x = a[i][ptrs[i]] # iの次候補
            if ptrs[x] == n - 1 or x in visited:
                continue

            y = a[x][ptrs[x]] # xの次候補
            if i == y:
                ptrs[i] += 1
                ptrs[x] += 1
                visited.add(i)
                visited.add(x)
                ok = True

        # すべて未訪問の場合
        if not ok:
            return -1
        day += 1

print(f())

Fixed submission:
import collections

n = int(input())
a = [collections.deque(int(s) - 1 for s in input().split()) for _ in range(n)]

day = 0
players = set(range(n))
while any(v for v in a):
    nxt_players = set()
    for player in players:
        if len(a[player]) == 0 or player in nxt_players:
            continue
        
        other = a[player][0]
        if len(a[other]) == 0 or other in nxt_players:
            continue
        
        if player == a[other][0]:
            a[player].popleft()
            a[other].popleft()
            nxt_players.add(player)
            nxt_players.add(other)

    if nxt_players:
        day += 1
        players = nxt_players
    else:
        print(-1)
        exit()

print(day)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [list(map(lambda s: int(s) - 1, input().split())) for _ in range(n)]

def f():
    day = 0
    ptrs = [0] * n

    while True:
        if all(ptr == n - 1 for ptr in ptrs):
            return day

        visited = [False] * n
        for i in range(n):
            if ptrs[i] == n - 1 or visited[i]:
                continue
            
            x = a[i][ptrs[i]] # iの次候補
            if ptrs[x] == n - 1 or visited[x]:
                continue

            y = a[x][ptrs[x]] # xの次候補
            if i == y:
                ptrs[i] += 1
                ptrs[x] += 1
                visited[i] = True
                visited[x] = True

        # すべて未訪問の場合
        if all(not v for v in visited):
            return -1
        day += 1

print(f())

Fixed submission:
import collections

n = int(input())
a = [collections.deque(int(s) - 1 for s in input().split()) for _ in range(n)]

day = 0
players = set(range(n))
while any(v for v in a):
    nxt_players = set()
    for player in players:
        if len(a[player]) == 0 or player in nxt_players:
            continue
        
        other = a[player][0]
        if len(a[other]) == 0 or other in nxt_players:
            continue
        
        if player == a[other][0]:
            a[player].popleft()
            a[other].popleft()
            nxt_players.add(player)
            nxt_players.add(other)

    if nxt_players:
        day += 1
        players = nxt_players
    else:
        print(-1)
        exit()

print(day)