Buggy python submission (verdict: Time Limit Exceeded):
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

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

def main():
  input = sys.stdin.readline
  n = int(input())
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = set()
    flag = True
    for v in check:
      if v in already:
        continue
      flag = False
      v2 = a[v][index[v]]
      if v2 in already:
        continue
      if v != a[v2][index[v2]]:
        continue
      already = already|{v, v2}
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print(-1)
      sys.exit()
    if count == n:
      break
  print(ans)

  
if __name__ == "__main__":
  main()

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

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

def main():
  input = sys.stdin.readline
  n = int(input())
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = set()
    flag = True
    for v in check:
      if v in already:
        continue
      flag = False
      v2 = a[v][index[v]]
      if v2 in already:
        continue
      if v != a[v2][index[v2]]:
        continue
      already = already|{v, v2}
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print(-1)
      sys.exit()
    if count == n:
      break
  
  print(ans)
  

if __name__ == "__main__":
  main()

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

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

def main():
  input = sys.stdin.readline
  n = int(input())
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = [False]*n
    flag = True
    for v in check:
      if already[v]:
        continue
      flag = False
      v2 = a[v][index[v]]
      if already[v2]:
        continue
      if v != a[v2][index[v2]]:
        continue
      already[v] = True
      already[v2] = True
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print(-1)
      sys.exit()
    if count == n:
      break
  
  print(ans)
  

if __name__ == "__main__":
  main()


Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

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

def main():
  input = sys.stdin.readline
  n = int(input())
  a = [None]*n
  for i in range(n):
    a[i] = [int(x)-1 for x in input().split()]
  
  count = 0
  ans = 0
  bef_checked = [int(x) for x in range(n)]
  index = [0]*n
  while True:
    ans += 1
    check = copy.deepcopy(bef_checked)
    bef_checked = []
    already = [False]*n
    flag = True
    for v in check:
      if already[v]:
        continue
      flag = False
      v2 = a[v][index[v]]
      if already[v2]:
        continue
      if v != a[v2][index[v2]]:
        continue
      already[v] = True
      already[v2] = True
      index[v] += 1
      index[v2] += 1
      if index[v] == n-1:
        count += 1
      else:
        bef_checked.append(v)
      if index[v2] == n-1:
        count += 1
      else:
        bef_checked.append(v2)
        
    if flag:
      print(-1)
      sys.exit()
    if count == n:
      break
  
  print(ans)
  

if __name__ == "__main__":
  main()

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import copy
n = int(input())

games = [0]*n

for i in range(n):
  games[i] = deque([int(x) for x in input().split()])

ans = 0
already = [False]*n
fin = 0
rev=0
while True:
  flag = False
  key = copy.deepcopy(already)
  if rev%2:
    for i in range(n):
      if key[i]:
        continue
      k = games[i][0]-1
      if key[k]:
        continue
      if games[k][0] == i+1:
        flag = True
        key[k] = True
        key[i] = True
        games[k].popleft()
        games[i].popleft()
        if len(games[k]) == 0:
          already[k] = True
          fin += 1
        if len(games[i]) == 0:
          already[i] = True
          fin += 1
  else:
    for i in range(n-1, -1, -1):
      if key[i]:
        continue
      k = games[i][0]-1
      if key[k]:
        continue
      if games[k][0] == i+1:
        flag = True
        key[k] = True
        key[i] = True
        games[k].popleft()
        games[i].popleft()
        if len(games[k]) == 0:
          already[k] = True
          fin += 1
        if len(games[i]) == 0:
          already[i] = True
          fin += 1
  if flag:
    ans += 1
  else:
    ans = -1
    break
  if fin == n:
    break
  rev += 1
  
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import copy
n = int(input())

games = [0]*n

for i in range(n):
  games[i] = deque([int(x) for x in input().split()])

ans = 0
already = [False]*n
fin = 0
rev=0
while True:
  flag = False
  key = copy.deepcopy(already)
  if rev%2:
    for i in range(n):
      if key[i]:
        continue
      k = games[i][0]-1
      if key[k]:
        continue
      if games[k][0] == i+1:
        flag = True
        key[k] = True
        key[i] = True
        games[k].popleft()
        games[i].popleft()
        if len(games[k]) == 0:
          already[k] = True
          fin += 1
        if len(games[i]) == 0:
          already[i] = True
          fin += 1
  else:
    for i in range(n-1, -1, -1):
      if key[i]:
        continue
      k = games[i][0]-1
      if key[k]:
        continue
      if games[k][0] == i+1:
        flag = True
        key[k] = True
        key[i] = True
        games[k].popleft()
        games[i].popleft()
        if len(games[k]) == 0:
          already[k] = True
          fin += 1
        if len(games[i]) == 0:
          already[i] = True
          fin += 1
  if flag:
    ans += 1
  else:
    ans = -1
    break
  if fin == n:
    break
  rev += 1
  
print(ans)




Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import copy
n = int(input())

games = [0]*n

for i in range(n):
  games[i] = deque([int(x) for x in input().split()])

ans = 0
already = [False]*n
fin = 0
while True:
  flag = False
  key = copy.deepcopy(already)
  for i in range(n):
    if key[i]:
      continue
    k = games[i][0]-1
    if key[k]:
      continue
    if games[i][0] > i+1 and games[k][0] == i+1:
      flag = True
      key[k] = True
      games[k].popleft()
      games[i].popleft()
      if len(games[k]) == 0:
        already[k] = True
        fin += 1
      if len(games[i]) == 0:
        already[i] = True
        fin += 1
  if flag:
    ans += 1
  else:
    ans = -1
    break
  if fin == n:
    break
    
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import copy
n = int(input())

games = [0]*n

for i in range(n):
  games[i] = deque([int(x) for x in input().split()])

ans = 0
already = [False]*n
fin = 0
while True:
  flag = False
  key = copy.deepcopy(already)
  for i in range(n):
    if key[i]:
      continue
    k = games[i][0]-1
    if key[k]:
      continue
    if games[i][0] > i+1 and games[k][0] == i+1:
      flag = True
      key[k] = True
      games[k].popleft()
      games[i].popleft()
      if len(games[k]) == 0:
        already[k] = True
        fin += 1
      if len(games[i]) == 0:
        already[i] = True
        fin += 1
  if flag:
    ans += 1
  else:
    ans = -1
    break
  if fin == n:
    break
    
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import collections
n = int(input())
a = collections.deque()
for i in range(n):
    k = collections.deque([int(x) for x in input().split()])
    a.append(k)
ans = 0
key = 0
while a:
    s = 1
    todaylist = []
    for i in range(n):
        if i+1 in todaylist:
            continue
        if a[i]:
            if a[i][0] in todaylist:
                continue
            b = a[i][0]-1
            if a[b]:
                if a[b][0] == i+1:
                    a[i].popleft()
                    a[b].popleft()
                    todaylist.append(i+1)
                    todaylist.append(b+1)
                    s = 0
    ans += 1
    key += 1
    if s:
        print(-1)
        sys.exit()
    if key >= n-1 and not any(a):
        break
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a = []
for i in range(n):
    k = [int(x) for x in input().split()]
    j = []
    for i in range(1, len(k)+1):
        j.append(k[-i])
    a.append(j)
ans = 0
while a:
    s = 1
    todaylist = []
    for i in range(n):
        if i+1 in todaylist:
            continue
        if a[i]:
            if a[i][-1] in todaylist:
                continue
            b = a[i][-1]-1
            if a[b]:
                if a[b][-1] == i+1:
                    a[i].pop()
                    a[b].pop()
                    todaylist.append(i+1)
                    todaylist.append(b+1)
                    s = 0
    ans += 1
    if s:
        print(-1)
        sys.exit()
    if not any(a):
        break
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a = []
for i in range(n):
    k = [int(x) for x in input().split()]
    j = []
    for i in range(1, len(k)+1):
        j.append(k[-i])
    a.append(j)
ans = 0
while a:
    todaylist = []
    s = 1
    for i in range(n):
        if a[i]:
            p = [min([i+1, a[i][-1]]), max([i+1, a[i][-1]])]
            if p in todaylist:
                a[p[0]-1].pop()
                a[p[1]-1].pop()
                s = 0
            else:
                todaylist.append([min([i+1,a[i][-1]]),max([i+1,a[i][-1]])])
    ans += 1
    if s:
        print(-1)
        sys.exit()
    if not any(a):
        break
print(ans)

Fixed submission:
import sys
from copy import deepcopy as copy

def main():
    input = sys.stdin.readline
    n = int(input())
    a = [None]*n
    for i in range(n):
        a[i] = [int(x)-1 for x in input().split()]
    
    index = [0]*n
    search = [int(x) for x in range(n)]
    ans = 0
    while True:
        already = set()
        judge = True
        sub = []
        for key in search:
            if index[key] == n-1:
                continue
            key2 = a[key][index[key]]
            if index[key2] == n-1:
                continue
            if key != a[key2][index[key2]]:
                continue
            if key in already or key2 in already:
                continue
            already.add(key)
            already.add(key2)
            index[key] += 1
            index[key2] += 1
            sub.append(key)
            sub.append(key2)
            judge = False
        if judge:
            break
        ans += 1
        search = copy(sub)
        
    for value in index:
        if value != n-1:
            print(-1)
            sys.exit()
    print(ans)
    
if __name__ == "__main__":
    main()