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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    stamina=[1]*N
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
n=0
first_fight = set([i for i in range(N)])
record = set()
first = True

while True:
    change=False
    stamina=[1]*N
    if first is True:
        pre_fight = first_fight
    else:
        pre_fight = record
        record = set()
    for g in pre_fight:
        if A[g]!=deque() and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].popleft()
                A[_pop-1].popleft()
                stamina[g]=0
                stamina[_pop-1]=0
                record.add(g)
                record.add(_pop-1)
                change=True
                n+=1
    first=False
    ans+=1
    if change is False:
        break
    if n==N*(N-1)//2:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for n in range(N)]
first_fight = set([i for i in range(N)])
record = set()

#前日に試合をした選手のみにマッチングを確認する(これで最悪O(n^2))。
#そうしないと、「while True」の部分で最悪n(n-1)/2の計算量がかかり、O(n^3)となって詰む
first = True

while True:
    change=False
    stamina=[1]*N
    if first is True:
        pre_fight = first_fight
    else:
        pre_fight = record
        record = set()
    for g in pre_fight:
        if A[g]!=deque() and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].popleft()
                A[_pop-1].popleft()
                stamina[g]=0
                stamina[_pop-1]=0
                change=True
                record.add(g)
                record.add(_pop-1)
    first=False
    ans+=1
    if change is False:
        break
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for n in range(N)]
first_fight = set([i for i in range(N)])
record = set()

#前日に試合をした選手のみマッチングを確認する(これで最悪O(n^2))。
#そうしないと、「while True」の部分で最悪n(n-1)/2の計算量がかかり、O(n^3)となって詰む
while True:
    first = True
    change=False
    stamina=[1]*N
    if first is True:
        pre_fight = first_fight
    else:
        pre_fight = record
    for g in pre_fight:
        if A[g]!=deque() and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].popleft()
                A[_pop-1].popleft()
                stamina[g]=0
                stamina[_pop-1]=0
                change=True
                record.add(g)
    first=False
    record = set()
    ans+=1
    if change is False:
        break
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for n in range(N)]

while True:
    change=0
    stamina=[1]*N
    for g in range(N):
        if A[g]!=deque() and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].popleft()
                A[_pop-1].popleft()
                stamina[g]=0
                stamina[_pop-1]=0
                change=1
    ans+=1
    if change==0:
        break
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for n in range(N)]

while True:
    change=0
    stamina=[1]*N
    for g in range(N):
        if A[g]!=deque() and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].popleft()#popはゴミ
                A[_pop-1].popleft()
                stamina[g]=0
                stamina[_pop-1]=0
                change+=1
    ans+=1
    if change==0:
        break
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [[int(i) for i in input().split()] for i in range(N)]

ans=0
empty = [[] for n in range(N)]

for i in range(N*(N-1)//2):
    change=0
    stamina=[1]*N
    for g in range(N):
        if A[g]!=[] and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].pop(0)
                A[_pop-1].pop(0)
                stamina[g]=0
                stamina[_pop-1]=0
                change+=1
    ans+=1
    if change==0:
        break
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)

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

N = int(input())
A = [[int(i) for i in input().split()] for i in range(N)]

ans=0

for i in range(N*(N-1)//2):
    stamina=[1]*N
    for g in range(N):
        if A[g]!=[] and stamina[g]==1 and stamina[A[g][0]-1]==1:
            if g+1 == A[A[g][0]-1][0]:
                _pop = A[g].pop(0)
                A[_pop-1].pop(0)
                stamina[g]=0
                stamina[_pop-1]=0
    ans+=1
    if A==[[] for n in range(N)]:
        print(ans)
        sys.exit()

print(-1)

Fixed submission:
import sys
from collections import deque

N = int(input())
A = [deque([int(i) for i in input().split()]) for i in range(N)]

ans=0
empty = [deque() for i in range(N)]
pre_fight = set([i for i in range(N)])
record = set()

while True:
    for g in pre_fight:
        if A[g]!=deque():
            if g+1 == A[A[g][0]-1][0]:
                record.add(g)
                record.add(A[g][0]-1)
    if record==set():
        break
    else:
        for i in record:
            A[i].popleft()
    ans+=1
    pre_fight = record
    record = set()
    if A==empty:
        print(ans)
        sys.exit()

print(-1)