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

N=int(input())
A=[list(map(int,input().split())) for _ in range(N)]
A=list(map(lambda x: deque(x), A))
#print(A)

ans=0

L=[]
for i in range(N):
  L.append(len(A[i]))
#print(L)

while 1:
  P=[0]*N
  sw=0
  C=[]
  for i in range(N):
    if L[i]:
      if i+1==A[A[i][0]-1][0]:
        P[i]=1
        C.append(i)
        sw=1
  #print(P)
  if sw:
    for c in C:
      if P[c]:
        A[c].popleft()
        L[c]-=1
    ans+=1
  else:
    tmp=0
    for a in A:
      tmp+=len(a)
    if tmp!=0:
      ans=-1
    break
print(ans)
    

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

N = int(input())
A = [[]] + [list(map(int, input().split()))[::-1] for _ in range(N)]

pair = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

stack = []
for i in range(1, N + 1):
  a = A[i].pop()
  pair[i][a] += 1
  if pair[i][a] == pair[a][i] == 1:
    stack.append(i)
    stack.append(a)

r = []
answer = 1
while stack:
  while stack:
    i = stack.pop()
    if A[i]:
      a = A[i].pop()
      pair[i][a] += 1
      if pair[i][a] == pair[a][i] == 1:
        r.append(i)
        r.append(a)
  stack = r
  if r:
    answer += 1
  r = []
  
flag = 1
for i in range(1, N + 1):
  if A[i]:
    flag = 0
    break
if flag:
  print(answer)
else:
  print(-1)

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

N=int(input())
A=[list(map(int,input().split())) for _ in range(N)]
A=list(map(lambda x: deque(x), A))
#print(A)

ans=0

L=[]
for i in range(N):
  L.append(len(A[i]))
#print(L)

while 1:
  P=[0]*N
  sw=0
  for i in range(N):
    if L[i]:
      if i+1==A[A[i][0]-1][0]:
        P[i]=1
        sw=1
  #print(P)
  if sw:
    for i in range(N):
      if P[i]:
        A[i].popleft()
        L[i]-=1
    ans+=1
  else:
    tmp=0
    for a in A:
      tmp+=len(a)
    if tmp!=0:
      ans=-1
    break
print(ans)
    

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

N = int(input())
A = [[]] + [list(map(int, input().split()))[::-1] for _ in range(N)]

pair = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

stack = []
for i in range(1, N + 1):
  a = A[i].pop()
  pair[i][a] += 1
  if pair[i][a] == pair[a][i] == 1:
    stack.append(i)
    stack.append(a)

r = []
answer = 1
while stack:
  while stack:
    i = stack.pop()
    if A[i]:
      a = A[i].pop()
      pair[i][a] += 1
      if pair[i][a] == pair[a][i] == 1:
        r.append(i)
        r.append(a)
  stack = r
  if r:
    answer += 1
  r = []
  
flag = 1
for i in range(1, N + 1):
  if A[i]:
    flag = 0
    break
if flag:
  print(answer)
else:
  print(-1)

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

N=int(input())
A=[list(map(int,input().split())) for _ in range(N)]
A=list(map(lambda x: deque(x), A))
#print(A)

ans=0
while 1:
  P=[0]*N
  for i in range(N):
    if len(A[i]):
      if i+1==A[A[i][0]-1][0]:
        P[i]=1
  #print(P)
  if sum(P):
    for i in range(N):
      if P[i]:
        A[i].popleft()
    ans+=1
  else:
    tmp=0
    for a in A:
      tmp+=len(a)
    if tmp!=0:
      ans=-1
    break
print(ans)
    

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

N = int(input())
A = [[]] + [list(map(int, input().split()))[::-1] for _ in range(N)]

pair = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

stack = []
for i in range(1, N + 1):
  a = A[i].pop()
  pair[i][a] += 1
  if pair[i][a] == pair[a][i] == 1:
    stack.append(i)
    stack.append(a)

r = []
answer = 1
while stack:
  while stack:
    i = stack.pop()
    if A[i]:
      a = A[i].pop()
      pair[i][a] += 1
      if pair[i][a] == pair[a][i] == 1:
        r.append(i)
        r.append(a)
  stack = r
  if r:
    answer += 1
  r = []
  
flag = 1
for i in range(1, N + 1):
  if A[i]:
    flag = 0
    break
if flag:
  print(answer)
else:
  print(-1)