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

def main():    
    nums = {}
    N = int(input())
    count = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            nums[(i, j)] = count
            count += 1
    
    graph = {i:deque() for i in range(count)}
    input_num = [0] * count
    
    for i in range(1, N+1):
        A = list(map(int, input().split()))
    
        for j in range(N-2):
            a, b = i, A[j]
            if a > b: a, b = b, a
            
            c, d = i, A[j+1]
            if c > d: c, d = d, c
        
            n = nums[(c, d)]        
            graph[nums[(a, b)]].append(n)
            input_num[n] += 1
        
    q = deque([(i, 1) for i in range(count) if not input_num[i]])
    size = len(q)
    dist = [0] * count
    
    while q:
        v, c = q.popleft()
        c += 1
        for u in graph[v]:
            input_num[u] -= 1
            dist[u] = max(dist[u], c)
            if not input_num[u]:
                q.append((u, c))
                size += 1
    
    if size != count: ans = -1
    else: ans = max(dist)
    
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

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

def main():
    N = int(input())
    A = [None] + [deque(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    base_used = {i for i in range(1, N+1)}
        
    while not finish and base_used:
        finish = True
        ans += 1
        used = set()
        delete_used = set()
        
        for n in base_used:
            if n in delete_used:
                continue
            
            a = A[n][0]
            if not a in used and not n in used and A[a][0] == n:
                for x in [n, a]:
                    used.add(x)
                    A[x].popleft()
    
                    if not A[x]:
                        delete_used.add(x)
                finish = False     
        base_used -= delete_used
        
    if base_used:
        ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()


Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

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

def main():
    N = int(input())
    A = [deque(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    base_used = [True] + [False] * N
    count = 0
    
    while not finish and count != N:
        finish = True
        ans += 1
        used = copy.deepcopy(base_used)
        
        for idx, u in enumerate(used):
            if not u:
                a = A[idx-1][0]
                if not used[a] and A[a-1][0] == idx:
                    for x in [idx, a]:
                        used[x] = True
                        A[x-1].popleft()

                        if not A[x-1]:
                            base_used[x] = True
                            count += 1
                    finish = False     
    
    if N != count:
        ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import copy

def main():
    N = int(input())
    A = [list(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    base_used = [True] + [False] * N
    count = 0
    
    while not finish and count != N:
        finish = True
        ans += 1
        used = copy.deepcopy(base_used)
        
        for idx, u in enumerate(used):
            if not u:
                a = A[idx-1][0]
                if not used[a] and A[a-1][0] == idx:
                    for x in [idx, a]:
                        used[x] = True
                        A[x-1].pop(0)
                        
                        if not A[x-1]:
                            base_used[x] = True
                            count += 1
                    finish = False     
    
    if N != count:
      ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

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

def main():
    N = int(input())
    A = [deque(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    base_used = [True] + [False] * N
    count = 0
    
    while not finish and count != N:
      finish = True
      ans += 1
      used = copy.deepcopy(base_used)
      
      for idx, u in enumerate(used):
        if not u:
          a = A[idx-1][0]
          if not used[a] and A[a-1][0] == idx:
            used[idx] = True
            used[a] = True
            A[idx-1].popleft()
            A[a-1].popleft()
            finish = False
            
            if not A[idx-1]:
                base_used[idx] = True
                count += 1
                
            if not A[a-1]:
                base_used[a] = True
                count += 1
     
    if N != count:
      ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

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

def main():
    N = int(input())
    A = [deque(map(int, input().split())) for i in range(N)]
    
    ans = 0
    finish = False
    q = deque()
    base_used = [True] + [False] * N
    count = 0
    
    while not finish and count != N:
      finish = True
      ans += 1
      used = copy.deepcopy(base_used)
      
      for idx, u in enumerate(used):
        if not u:
          a = A[idx-1][0]
          if not used[a] and A[a-1][0] == idx:
            used[idx] = True
            used[a] = True
            q.extend([idx, a])
            finish = False
        else:
          continue
      
      while q:
        a = q.popleft()-1
        A[a].popleft()
        if not A[a]:
          base_used[a+1] = True
          count += 1
    
    if N != count:
      ans = -1
    print(ans)
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import copy

N = int(input())
A = [list(map(int, input().split())) for i in range(N)]

ans = 0
finish = False
q = []
base_used = [True] + [False] * N
count = 0

while not finish and count != N:
  finish = True
  ans += 1
  used = copy.deepcopy(base_used)
  
  for idx, u in enumerate(used):
    if not u:
      a = A[idx-1][0]
      if not used[a] and A[a-1][0] == idx:
        used[idx] = True
        used[a] = True
        q.extend([idx, a])
        finish = False
    else:
      continue
  
  while q:
    a = q.pop(0)-1
    A[a].pop(0)
    if not A[a]:
      base_used[a+1] = True
      count += 1

if N != count:
  ans = -1
print(ans)

Fixed submission:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    
    Id = {}
    
    cur = 0
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            Id[(i,j)] = cur
            cur += 1
            
    G = [[] for i in range(N*(N-1)//2)]
    size = [0] * (N*(N-1)//2)
    
    for i in range(1, N+1):
        for idx, j in enumerate(list(map(int, input().split()))):
            if i < j: k = Id[(i, j)]
            else: k = Id[(j, i)]
            if idx: 
                G[par].append(k)
                size[k] += 1
            par = k
            
    g = [idx for idx, s in enumerate(size) if s == 0]
    
    ans = 0
    while g:
        ans += 1
        n_g = []
        
        for v in g:
            length = len(G[v])
            if not length: continue
            
            size[G[v][0]] -= 1
            if not size[G[v][0]]: n_g.append(G[v][0])
    
            if length == 2:
                size[G[v][1]] -= 1
                if not size[G[v][1]]: n_g.append(G[v][1])
        g = n_g
        
    for s in size:
        if s != 0:
            print(-1)
            sys.exit()
    print(ans)
            
if __name__ == '__main__':
    main()