Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
 
N = int(input())
As = [[]]
iset = [0]*(N+1)
for _ in range(N):
  As.append(list(map(int, input().split())))

cnt = 0
eset = set()
while len(eset) < N:
  cnt += 1
  fset = set()
  f = 1
  for i in range(1, N+1):
    if i not in eset and i not in fset:
      vis = As[i][iset[i]]
      if As[vis][iset[vis]] == i and vis not in fset:
        iset[i] += 1
        iset[vis] += 1
        fset.add(i)
        fset.add(vis)
        if iset[i] == N-1:
          eset.add(i)
        if iset[vis] == N-1:
          eset.add(vis)
        f = 0
  if f == 1:
    print('-1')
    exit()
      
print(cnt)

Fixed submission:
from collections import deque
 
N = int(input())
As = [[]]
iset = [0]*(N+1)
for _ in range(N):
  As.append(list(map(int, input().split())))

cnt = 0
cset = set([i for i in range(1,N+1)])
while len(cset) > 0:
  cnt += 1
  fset = set()
  nex = set()
  f = 1
  for i in cset:
    if i not in fset:
      vis = As[i][iset[i]]
      if As[vis][iset[vis]] == i and vis not in fset:
        iset[i] += 1
        iset[vis] += 1
        fset.add(i)
        fset.add(vis)
        if iset[i] < N-1:
          nex.add(i)
        if iset[vis] < N-1:
          nex.add(vis)
        f = 0
  cset = nex
  if f == 1:
    print('-1')
    exit()
    
print(cnt)

Buggy python submission (verdict: Runtime Error):
N = int(input())

arr = {(i,j):set([]) for i in range(1,N+1) for j in range(i+1,N+1)}
dic = {(i,j):0 for i in range(1,N+1) for j in range(i+1,N+1)}
used = {(i,j):0 for i in range(1,N+1) for j in range(i+1,N+1)}
visited = {(i,j):0 for i in range(1,N+1) for j in range(i+1,N+1)}
rlt = {(i,j):0 for i in range(1,N+1) for j in range(i+1,N+1)}

def dfs(t):
  for u in arr[t]:
    if visited[u] == 1 and used[u] == 0:
      return -1
    rlt[u] = max(rlt[u], rlt[t]+1)
    if visited[u] == 0:
      visited[u] = 1
      dfs(u)
  used[t] = 1
  return 0

for i in range(1,N+1):
  As = map(int, input().split())
  b = 0
  for a in As:
    if b > 0:
      if i < a and i < b:
        arr[(i,b)].add((i,a))
        dic[(i,a)] += 1
      elif i > a and i < b:
        arr[(i,b)].add((a,i))
        dic[(a,i)] += 1
      elif i < a and i > b:
        arr[(b,i)].add((i,a))
        dic[(i,a)] += 1
      elif i > a and i > b:
        arr[(b,i)].add((a,i))
        dic[(a,i)] += 1
    b = a
    
cnt = N+1
for t in rlt:
  cnt = min(cnt, dic[t])
if cnt > 0:
  print(-1)
  exit()
  
cnt = 0
for t in rlt:
  if dic[t] > 0:
    continue
  rlt[t] = 1
  r = dfs(t)
  if r == -1:
    print(-1)
    exit()
        
for t in rlt:
  cnt = max(rlt[t], cnt)
print(cnt)

Fixed submission:
from collections import deque
 
N = int(input())
As = [[]]
iset = [0]*(N+1)
for _ in range(N):
  As.append(list(map(int, input().split())))

cnt = 0
cset = set([i for i in range(1,N+1)])
while len(cset) > 0:
  cnt += 1
  fset = set()
  nex = set()
  f = 1
  for i in cset:
    if i not in fset:
      vis = As[i][iset[i]]
      if As[vis][iset[vis]] == i and vis not in fset:
        iset[i] += 1
        iset[vis] += 1
        fset.add(i)
        fset.add(vis)
        if iset[i] < N-1:
          nex.add(i)
        if iset[vis] < N-1:
          nex.add(vis)
        f = 0
  cset = nex
  if f == 1:
    print('-1')
    exit()
    
print(cnt)

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

N = int(input())
As = [deque([])]
for _ in range(N):
  As.append(deque(list(map(int, input().split()))))

cnt = 0
eset = set()
while len(eset) < N:
  cnt += 1
  fset = set()
  f = 1
  for i in range(1, N+1):
    if len(As[i]) == 0:
      eset.add(i)
    if i not in eset and i not in fset:
      if As[As[i][0]][0] == i and As[i][0] not in fset:
        fset.add(As[As[i][0]].popleft())
        fset.add(As[i].popleft())
        if len(As[i]) == 0:
          eset.add(i)
        f = 0
  if f == 1:
    print('-1')
    exit()
      
print(cnt)

Fixed submission:
from collections import deque
 
N = int(input())
As = [[]]
iset = [0]*(N+1)
for _ in range(N):
  As.append(list(map(int, input().split())))

cnt = 0
cset = set([i for i in range(1,N+1)])
while len(cset) > 0:
  cnt += 1
  fset = set()
  nex = set()
  f = 1
  for i in cset:
    if i not in fset:
      vis = As[i][iset[i]]
      if As[vis][iset[vis]] == i and vis not in fset:
        iset[i] += 1
        iset[vis] += 1
        fset.add(i)
        fset.add(vis)
        if iset[i] < N-1:
          nex.add(i)
        if iset[vis] < N-1:
          nex.add(vis)
        f = 0
  cset = nex
  if f == 1:
    print('-1')
    exit()
    
print(cnt)