Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append([x,tmp])
        return

def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = get_unique_list(q)
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)


Fixed submission:
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque

N=int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
lencount = 0
ans = 0
while lencount < N:
    update = False
    done = [0]*N
    for i in range(N):
        if len(A[i]) == 0 or done[i] == 1: continue
        tmp = A[i][0]-1
        if len(A[tmp]) == 0 or done[tmp] == 1: continue
        if A[tmp][0]-1 == i:
            update = True
            done[i] = 1
            done[tmp] = 1
            A[i].popleft()
            A[tmp].popleft()
            if len(A[i]) == 0:
                lencount+=1
            if len(A[tmp]) == 0:
                lencount+=1
    ans += 1
    if not(update):
        print(-1)
        sys.exit()
print(ans)
    







Fixed submission:
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque

N=int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
lencount = 0
ans = 0
while lencount < N:
    update = False
    done = [0]*N
    for i in range(N):
        if len(A[i]) == 0 or done[i] == 1: continue
        tmp = A[i][0]-1
        if len(A[tmp]) == 0 or done[tmp] == 1: continue
        if A[tmp][0]-1 == i:
            update = True
            done[i] = 1
            done[tmp] = 1
            A[i].popleft()
            A[tmp].popleft()
            if len(A[i]) == 0:
                lencount+=1
            if len(A[tmp]) == 0:
                lencount+=1
    ans += 1
    if not(update):
        print(-1)
        sys.exit()
print(ans)
    







Fixed submission:
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())

A = [deque(map(int, input().split())) for _ in range(N)]

ans = 0

len_count = [0]*N
lencount=0
while lencount < N:
    update = False
    done = [0]*(N)
    for i in range(N):
        if done[i] == 1: continue
        if len(A[i]) == 0: continue
        tmp = A[i][0]-1
        if done[tmp] == 1: continue
        if len(A[tmp]) !=0 and A[tmp][0]-1 == i:
            update = True
            done[i] = 1
            done[tmp] = 1
            A[i].popleft()
            A[tmp].popleft()
            if len(A[i])==0 and len_count[i]==0:
                len_count[i] = 1
                lencount+=1
            if len(A[tmp])==0 and len_count[tmp]==0:
                len_count[tmp] = 1
                lencount+=1
    #print(lencount)
    if not(update):
        print(-1)
        sys.exit()
    ans+=1
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())

A = [deque(map(int, input().split())) for _ in range(N)]

ans = 0

len_count = [0]*N
while sum(len_count) < N:
    update = False
    done = [0]*(N)
    for i in range(N):
        if done[i] == 1: continue
        if len(A[i]) == 0: continue
        tmp = A[i][0]-1
        if done[tmp] == 1: continue
        if len(A[tmp]) !=0 and A[tmp][0]-1 == i:
            update = True
            done[i] = 1
            done[tmp] = 1
            A[i].popleft()
            A[tmp].popleft()
            if len(A[i])==0:
                len_count[i] = 1
            if len(A[tmp])==0:
                len_count[tmp] = 1
    if not(update):
        print(-1)
        sys.exit()
    ans+=1
print(ans)


Fixed submission:
import sys
input = sys.stdin.readline

from collections import deque

N = int(input())
A = [deque(map(int, input().split())) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print(-1)
        sys.exit()
print(day)