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

cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    changed = False
    new_cur = cur[:]
    for i in range(N):
        if cur[i] == N-1:
            continue
        opponent =  A[i][cur[i]]
        if cur[opponent] == N-1:
            continue
        
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            new_cur[opponent] += 1
            cur[opponent] = N-1
            changed = True
            if new_cur[i] == N-1:
                goal += 1
            if new_cur[opponent] == N-1:
                goal += 1
                
    cur = new_cur
    if not changed:
        print(-1)
        exit()

print(day)


Fixed submission:
N = int(input())
A = []
for i in range(N):
    temp = list(map(int, input().split()))
    for j in range(N-1):
        temp[j] -= 1
    A.append(temp)

cand = list(range(N))
cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    game = []
    new_cur = cur[:]
    for i in cand:
        if i in game or cur[i] == N-1:
            continue
        opponent = A[i][cur[i]]
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            new_cur[opponent] += 1
            game.append(i)
            game.append(opponent)
            if new_cur[i] == N-1:
                goal += 1
            if new_cur[opponent] == N-1:
                goal += 1
    cand = game
    cur = new_cur
    if not game:
        print(-1)
        exit()

print(day)

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

cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    changed = False
    new_cur = cur[:]
    for i in range(N):
        if cur[i] == N-1:
            continue
        opponent =  A[i][cur[i]]
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            changed = True
            if new_cur[i] == N-1:
                goal += 1
    cur = new_cur
    if not changed:
        print(-1)
        exit()

print(day)


Fixed submission:
N = int(input())
A = []
for i in range(N):
    temp = list(map(int, input().split()))
    for j in range(N-1):
        temp[j] -= 1
    A.append(temp)

cand = list(range(N))
cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    game = []
    new_cur = cur[:]
    for i in cand:
        if i in game or cur[i] == N-1:
            continue
        opponent = A[i][cur[i]]
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            new_cur[opponent] += 1
            game.append(i)
            game.append(opponent)
            if new_cur[i] == N-1:
                goal += 1
            if new_cur[opponent] == N-1:
                goal += 1
    cand = game
    cur = new_cur
    if not game:
        print(-1)
        exit()

print(day)

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

cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    changed = False
    new_cur = cur[:]
    for i in range(N):
        if cur[i] == N-1:
            continue
        opponent =  A[i][cur[i]]
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            changed = True
            if new_cur[i] == N-1:
                goal += 1
    cur = new_cur
    if not changed:
        print(-1)
        exit()

print(day)


Fixed submission:
N = int(input())
A = []
for i in range(N):
    temp = list(map(int, input().split()))
    for j in range(N-1):
        temp[j] -= 1
    A.append(temp)

cand = list(range(N))
cur = [0] * N
goal = 0
day = 0
while goal < N:
    day += 1
    game = []
    new_cur = cur[:]
    for i in cand:
        if i in game or cur[i] == N-1:
            continue
        opponent = A[i][cur[i]]
        if A[opponent][cur[opponent]] == i:
            new_cur[i] += 1
            new_cur[opponent] += 1
            game.append(i)
            game.append(opponent)
            if new_cur[i] == N-1:
                goal += 1
            if new_cur[opponent] == N-1:
                goal += 1
    cand = game
    cur = new_cur
    if not game:
        print(-1)
        exit()

print(day)