Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
arr=[0]+[list(map(int,input().split())) for _ in range(n)]
pos=[0]*(n+1)
ans=0
while 1:
  flag=False
  cnt=0
  check=[0]*(n+1)
  for i in range(1,n+1):
    if check[i]==1:
      continue
    if pos[i]==n-1:
      cnt+=1
      continue
    pair=arr[i][pos[i]]
    if check[pair]==1:
      continue
    if arr[pair][pos[pair]]==i:
      flag=True
      pos[i]+=1
      pos[pair]+=1
      check[i]=1
      check[pair]=1
  if cnt==n:
    print(ans)
    break
  if flag==False:
    print(-1)
    break
  ans+=1

Fixed submission:
import collections

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=[0]+[collections.deque(arr[i]) for i in range(n)]
ans=0
cand=set(range(1,n+1))
while 1:
  check=set()
  for i in cand:
    if not arr[i]:
      continue
    pair=arr[i][0]
    if arr[pair][0]==i:
      check.add(i)
      check.add(pair)
  if not check:
    break
  cand=set()
  for i in check:
    cand.add(i)
    arr[i].popleft()
    if arr[i]:
      cand.add(arr[i][0])
  ans+=1
for i in range(1,n+1):
  if arr[i]:
    print(-1)
    break
else:
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
arr=[0]+[list(map(int,input().split())) for _ in range(n)]
pos=[0]*(n+1)
ans=0
while 1:
  flag=False
  cnt=0
  check=set()
  for i in range(1,n+1):
    if i in check:
      continue
    if pos[i]==n-1:
      cnt+=1
      continue
    pair=arr[i][pos[i]]
    if pair in check:
      continue
    if arr[pair][pos[pair]]==i:
      flag=True
      pos[i]+=1
      pos[pair]+=1
      check.add(i)
      check.add(pair)
  if cnt==n:
    print(ans)
    break
  if flag==False:
    print(-1)
    break
  ans+=1

Fixed submission:
import collections

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=[0]+[collections.deque(arr[i]) for i in range(n)]
ans=0
cand=set(range(1,n+1))
while 1:
  check=set()
  for i in cand:
    if not arr[i]:
      continue
    pair=arr[i][0]
    if arr[pair][0]==i:
      check.add(i)
      check.add(pair)
  if not check:
    break
  cand=set()
  for i in check:
    cand.add(i)
    arr[i].popleft()
    if arr[i]:
      cand.add(arr[i][0])
  ans+=1
for i in range(1,n+1):
  if arr[i]:
    print(-1)
    break
else:
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
arr=[0]+[list(map(int,input().split())) for _ in range(n)]
pos=[0]*(n+1)
ans=0
while 1:
  flag=False
  cnt=0
  check=set()
  for i in range(1,n+1):
    if i in check:
      continue
    if pos[i]==n-1:
      cnt+=1
      continue
    pair=arr[i][pos[i]]
    if pair in check:
      continue
    if arr[pair][pos[pair]]==i:
      flag=True
      pos[i]+=1
      pos[pair]+=1
      check.add(i)
      check.add(pair)
  if cnt==n:
    print(ans)
    break
  if flag==False:
    print(-1)
    break
  ans+=1

Fixed submission:
import collections

n=int(input())
arr=[list(map(int,input().split())) for _ in range(n)]
arr=[0]+[collections.deque(arr[i]) for i in range(n)]
ans=0
cand=set(range(1,n+1))
while 1:
  check=set()
  for i in cand:
    if not arr[i]:
      continue
    pair=arr[i][0]
    if arr[pair][0]==i:
      check.add(i)
      check.add(pair)
  if not check:
    break
  cand=set()
  for i in check:
    cand.add(i)
    arr[i].popleft()
    if arr[i]:
      cand.add(arr[i][0])
  ans+=1
for i in range(1,n+1):
  if arr[i]:
    print(-1)
    break
else:
  print(ans)