Buggy python submission (verdict: Time Limit Exceeded):
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Fixed submission:
#7271131(wotsushi)解答
n = int(input())
a = []
for _ in range(n):
  a.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = []
for i in range(n):
  j = a[i][cnt[i]]
  if a[j][cnt[j]] == i and i < j:
    now.append([i,j])
ans = 0
while now:
  #print(now)
  last = now
  now = []
  for x in last:
    i = x[0]
    j = x[1]
    cnt[i] += 1
    cnt[j] += 1
    if cnt[i] < n-1:
      k = a[i][cnt[i]]
      if a[k][cnt[k]] == i:
        now.append([i,k])
    if cnt[j] < n-1:
      k = a[j][cnt[j]]
      if a[k][cnt[k]] == j:
        now.append([j,k])
  ans += 1
if all(cnt[i] == n-1 for i in range(n)):
  print(ans)
else:
  print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    try:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
    except:
      pass
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Fixed submission:
#7271131(wotsushi)解答
n = int(input())
a = []
for _ in range(n):
  a.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = []
for i in range(n):
  j = a[i][cnt[i]]
  if a[j][cnt[j]] == i and i < j:
    now.append([i,j])
ans = 0
while now:
  #print(now)
  last = now
  now = []
  for x in last:
    i = x[0]
    j = x[1]
    cnt[i] += 1
    cnt[j] += 1
    if cnt[i] < n-1:
      k = a[i][cnt[i]]
      if a[k][cnt[k]] == i:
        now.append([i,k])
    if cnt[j] < n-1:
      k = a[j][cnt[j]]
      if a[k][cnt[k]] == j:
        now.append([j,k])
  ans += 1
if all(cnt[i] == n-1 for i in range(n)):
  print(ans)
else:
  print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
#16:07
n = int(input())
raw = []
for _ in range(n):
  raw.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = [i for i in range(n)]
ans = 0
while now:
  ans += 1
  last = now
  now = []
  tmp = [0 for _ in range(n)]
  for x in last:
    try:
      y = raw[x][cnt[x]]
      if tmp[x] == tmp[y] == 0 and x == raw[y][cnt[y]]:
        cnt[x] += 1
        cnt[y] += 1
        tmp[x] = 1
        tmp[y] = 1
        now.append(x)
        now.append(y)
    except:
      pass
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans-1)
else:
  print(-1)

Fixed submission:
#7271131(wotsushi)解答
n = int(input())
a = []
for _ in range(n):
  a.append(list(map(lambda x: int(x)-1, input().split())))
cnt = [0 for _ in range(n)]
now = []
for i in range(n):
  j = a[i][cnt[i]]
  if a[j][cnt[j]] == i and i < j:
    now.append([i,j])
ans = 0
while now:
  #print(now)
  last = now
  now = []
  for x in last:
    i = x[0]
    j = x[1]
    cnt[i] += 1
    cnt[j] += 1
    if cnt[i] < n-1:
      k = a[i][cnt[i]]
      if a[k][cnt[k]] == i:
        now.append([i,k])
    if cnt[j] < n-1:
      k = a[j][cnt[j]]
      if a[k][cnt[k]] == j:
        now.append([j,k])
  ans += 1
if all(cnt[i] == n-1 for i in range(n)):
  print(ans)
else:
  print(-1)