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

def dfs(g,d,O):
  if d<N:
    m=-1
    if len(O[g])==0:
      return d+1
    for i in O[g]:
      m=max(m,dfs(i,d+1,O))
      if m>N:
        break
    return m
  else:
    return N+1

def main():
  I=[[] for i in range(N*~-N//2)]
  O=[[] for i in range(N*~-N//2)]

  for i in range(N):
    pv=-1
    for j in [int(x)-1 for x in input().split()]:
      v=j*~-j//2+i if i<j else i*~-i//2+j
      if pv!=-1:
        I[v].append(pv)
        O[pv].append(v)
      pv=v

  m=-1
  for s in range(N*~-N//2):
    if len(I[s])!=0:
      continue
    m=max(m,dfs(s,0,O))
    if m>N:
      print(-1)
      exit(0)
  print(m)

main()


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
l=[]
s=set()

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]
l=[int(i) for i in range(n)]

t=0
f=True
while f==True:
  f=False
  s.clear()
  for i in l:
    if len(a[i])==0:
      continue
    f=True
    j=a[i][0]

    if i==a[j][0]:
      s.add(i)
      s.add(j)
  if f==False:
    break

  l = list(s)
  s.clear()
  if len(l)!=0:
    t+=1
    for i in range(len(l)):
      a[l[i]].pop(0)
      s.add(l[i])
      if len(a[l[i]])>0:
        s.add(a[l[i]][0])
  else:
    t = -1
    break
  l=list(s)

print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
l=[]

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  for i in range(n):
    if len(a[i])>0:
      f=True
      j=a[i][0]
      if i<j:
        if i==a[j][0]:
          l.append(i)
          l.append(j)

  if f==False:
    break

  t = t+1 if len(l)>0 else -1
  if len(l)==0:
    break;
  for i in range(len(l)):
    a[l.pop(0)].pop(0)

print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
l=[]

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  for i in range(n):
    if len(a[i])==0:
      continue
    f=True
    j=a[i][0]
    if i<j:
      if i==a[j][0]:
        l.append(i)
        l.append(j)

  if f==False:
    break

  if len(l)!=0:
    t+=1
    for i in range(len(l)):
      a[l.pop(0)].pop(0)
  else:
    t = -1
    break
print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
l=[]

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  for i in range(n):
    if len(a[i])==0:
      continue
    f=True
    if i==a[a[i][0]][0]:
      if i<a[i][0]:
        l.append(i)
        l.append(a[i][0])

  if f==False:
    break

  if len(l)!=0:
    t+=1
    for i in range(len(l)):
      a[l.pop(0)].pop(0)
  else:
    t = -1
    break
print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
s=set()

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  s.clear()
  for i in range(n):
    if len(a[i])==0:
      continue
    f=True
    if i==a[a[i][0]][0]:
      s.add(i)
      s.add(a[i][0])

  if f==False:
    break

  if len(s)!=0:
    t+=1
    l = list(s)
    for i in range(len(l)):
      a[l[i]].pop(0)
  else:
    t = -1
    break
print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
s=set()

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  s.clear()
  for i in range(n):
    if len(a[i])==0:
      continue
    f=True
    if len(a[a[i][0]])==0:
      continue
    if i==a[a[i][0]][0]:
      s.add(i)
      s.add(a[i][0])

  if f==False:
    break

  if len(s)!=0:
    t+=1
    l = list(s)
    for i in range(len(l)):
      a[l[i]].pop(0)
  else:
    t = -1
    break
print(t)


Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[[0]*n]*n
l=[]

for i in range(n):
  a[i]=[int(x)-1 for x in input().split()]

t=0
f=True
while f==True:
  f=False
  for i in range(n):
    if len(a[i])==0:
      continue
    f=True
    if len(a[a[i][0]])==0:
      continue
    if i==a[a[i][0]][0]:
      if not(i in l):
        l.append(i)
      if not(a[i][0] in l):
        l.append(a[i][0])

  if f==False:
    break

  if len(l)!=0:
    t+=1
    while len(l)!=0:
      a[l.pop(0)].pop(0)
      a[l.pop(0)].pop(0)
  else:
    t = -1
    break
print(t)
 

Fixed submission:
from collections import deque

def main():
  N=int(input())
  A=[deque(int(a)-1 for a in input().split()) for _ in range(N)]

  P,W=[0]*N,[-1]*N
  c=deque(range(N))

  while c:
    i=c.popleft()
    j=A[i].popleft()
    W[i]=j

    if W[j]==i:
      P[i]=P[j]=max(P[i],P[j])+1
      if A[i]:
        c.append(i)
      if A[j]:
        c.append(j)

  print(-1 if any(len(a) for a in A) else max(P))

main()