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

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
  for j in range(i+1, N):
    hash_table[i][j] = hash_table[j][i] = count
    count += 1


graph = [[] for i in range(m)]
before_this = [0] * m
for i in range(1, N+1):
  A = deque(list(map(int, input().split())))
  pre_a = A.popleft()
  pre_pair = hash_table[i-1][pre_a-1]
  for a in A:
    pair = hash_table[i-1][a-1]
    graph[pre_pair].append(pair)
    before_this[pair] += 1
    pre_pair = pair


games = deque([])
for i in range(m):
  if not before_this[i]:
    games.append(i)


if not games:
  ans = -1
else:
  ans = 0
  num_finished = 0
  while games:
    ans += 1
    num_games = len(games)
    num_finished += num_games
    for i in range(num_games):
      pair = games.popleft()
      subseq = graph[pair]
      for ss in subseq:
        before_this[ss] -= 1
        if not before_this[ss]:
          games.append(ss)
    
  if num_finished != m:
    ans = -1

print(ans)

Fixed submission:
from collections import deque

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
  for j in range(i+1, N):
    hash_table[i][j] = hash_table[j][i] = count
    count += 1


graph = [[] for i in range(m)]
before_this = [0] * m
for i in range(1, N+1):
  A = deque(list(map(int, input().split())))
  pre_a = A.popleft()
  pre_pair = hash_table[i-1][pre_a-1]
  for a in A:
    pair = hash_table[i-1][a-1]
    graph[pre_pair].append(pair)
    before_this[pair] += 1
    pre_pair = pair


games = deque([])
for i in range(m):
  if not before_this[i]:
    games.append(i)


if not games:
  ans = -1
else:
  ans = 0
  num_finished = 0
  while games:
    ans += 1
    num_games = len(games)
    num_finished += num_games
    for i in range(num_games):
      pair = games.popleft()
      subseq = graph[pair]
      for ss in subseq:
        before_this[ss] -= 1
        if not before_this[ss]:
          games.append(ss)
    
  if num_finished != m:
    ans = -1

print(ans)

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

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
    for j in range(i+1, N):
        hash_table[i][j] = hash_table[j][i] = count
        count += 1


graph = [[] for i in range(m)]
is_root = [True] * m
for i in range(1, N+1):
    A = deque(list(map(int, input().split())))
    pre_a = A.popleft()
    pre_pair = hash_table[i-1][pre_a-1]
    for a in A:
        pair = hash_table[i-1][a-1]
        graph[pre_pair].append(pair)
        is_root[pair] = False
        pre_pair = pair


stack = []
is_in_stack = [False] * m
for i in range(m):
    if is_root[i]:
        stack.append(i)
        is_in_stack[i] = True


if not stack:
    ans = -1
else:
  ans = 1
  depth = [1] * m
  while stack:
      pair = stack[-1]
      subseq = graph[pair]
      have_next = False
      for ss in subseq:
          if is_in_stack[ss]:
              ans = -1
              stack = []
              have_next = True
              break

          if have_next or depth[ss] > depth[pair]:
              continue

          have_next = True
          depth[ss] = depth[pair] + 1
          ans = max(ans, depth[ss])
          stack.append(ss)
          is_in_stack[ss] = True

      if not have_next:
          pair = stack.pop()
          is_in_stack[pair] = False

print(ans)

Fixed submission:
from collections import deque

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
  for j in range(i+1, N):
    hash_table[i][j] = hash_table[j][i] = count
    count += 1


graph = [[] for i in range(m)]
before_this = [0] * m
for i in range(1, N+1):
  A = deque(list(map(int, input().split())))
  pre_a = A.popleft()
  pre_pair = hash_table[i-1][pre_a-1]
  for a in A:
    pair = hash_table[i-1][a-1]
    graph[pre_pair].append(pair)
    before_this[pair] += 1
    pre_pair = pair


games = deque([])
for i in range(m):
  if not before_this[i]:
    games.append(i)


if not games:
  ans = -1
else:
  ans = 0
  num_finished = 0
  while games:
    ans += 1
    num_games = len(games)
    num_finished += num_games
    for i in range(num_games):
      pair = games.popleft()
      subseq = graph[pair]
      for ss in subseq:
        before_this[ss] -= 1
        if not before_this[ss]:
          games.append(ss)
    
  if num_finished != m:
    ans = -1

print(ans)

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

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
    for j in range(i+1, N):
        hash_table[i][j] = hash_table[j][i] = count
        count += 1


graph = [[] for i in range(m)]
is_root = [True] * m
for i in range(1, N+1):
    A = deque(list(map(int, input().split())))
    pre_a = A.popleft()
    pre_pair = hash_table[i-1][pre_a-1]
    for a in A:
        pair = hash_table[i-1][a-1]
        graph[pre_pair].append(pair)
        is_root[pair] = False
        pre_pair = pair


stack = []
is_in_stack = [False] * m
for i in range(m):
    if is_root[i]:
        stack.append(i)
        is_in_stack[i] = True


if not stack:
    ans = -1
else:
  ans = 1
  depth = [1] * m
  while stack:
      pair = stack[-1]
      subseq = graph[pair]
      have_next = False
      for ss in subseq:
          if is_in_stack[ss]:
              ans = -1
              stack = []
              have_next = True
              break

          if have_next or depth[ss] > depth[pair]:
              continue

          have_next = True
          depth[ss] = depth[pair] + 1
          ans = max(ans, depth[ss])
          stack.append(ss)
          is_in_stack[ss] = True

      if not have_next:
          pair = stack.pop()
          is_in_stack[pair] = False

print(ans)

Fixed submission:
from collections import deque

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
  for j in range(i+1, N):
    hash_table[i][j] = hash_table[j][i] = count
    count += 1


graph = [[] for i in range(m)]
before_this = [0] * m
for i in range(1, N+1):
  A = deque(list(map(int, input().split())))
  pre_a = A.popleft()
  pre_pair = hash_table[i-1][pre_a-1]
  for a in A:
    pair = hash_table[i-1][a-1]
    graph[pre_pair].append(pair)
    before_this[pair] += 1
    pre_pair = pair


games = deque([])
for i in range(m):
  if not before_this[i]:
    games.append(i)


if not games:
  ans = -1
else:
  ans = 0
  num_finished = 0
  while games:
    ans += 1
    num_games = len(games)
    num_finished += num_games
    for i in range(num_games):
      pair = games.popleft()
      subseq = graph[pair]
      for ss in subseq:
        before_this[ss] -= 1
        if not before_this[ss]:
          games.append(ss)
    
  if num_finished != m:
    ans = -1

print(ans)

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

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
    for j in range(i+1, N):
        hash_table[i][j] = hash_table[j][i] = count
        count += 1


graph = [[] for i in range(m)]
is_root = [True] * m
for i in range(1, N+1):
    A = deque(list(map(int, input().split())))
    pre_a = A.popleft()
    pre_pair = hash_table[i-1][pre_a-1]
    for a in A:
        pair = hash_table[i-1][a-1]
        graph[pre_pair].append(pair)
        is_root[pair] = False
        pre_pair = pair


stack = []
for i in range(m):
    if is_root[i]:
        stack.append(i)


if not stack:
    ans = -1
else:
  ans = 1
  depth = [1] * m
  while stack:
      pair = stack[-1]
      subseq = graph[pair]
      have_next = False
      for ss in subseq:
          if ss in stack:
              ans = -1
              stack = []
              have_next = True
              break

          if have_next or depth[ss] > depth[pair]:
              continue

          have_next = True
          depth[ss] = depth[pair] + 1
          ans = max(ans, depth[ss])
          stack.append(ss)

      if not have_next:
          stack.pop()

print(ans)

Fixed submission:
from collections import deque

N = int(input())
m = N*(N-1) // 2

hash_table = [[0]*N for i in range(N)]
count = 0
for i in range(N):
  for j in range(i+1, N):
    hash_table[i][j] = hash_table[j][i] = count
    count += 1


graph = [[] for i in range(m)]
before_this = [0] * m
for i in range(1, N+1):
  A = deque(list(map(int, input().split())))
  pre_a = A.popleft()
  pre_pair = hash_table[i-1][pre_a-1]
  for a in A:
    pair = hash_table[i-1][a-1]
    graph[pre_pair].append(pair)
    before_this[pair] += 1
    pre_pair = pair


games = deque([])
for i in range(m):
  if not before_this[i]:
    games.append(i)


if not games:
  ans = -1
else:
  ans = 0
  num_finished = 0
  while games:
    ans += 1
    num_games = len(games)
    num_finished += num_games
    for i in range(num_games):
      pair = games.popleft()
      subseq = graph[pair]
      for ss in subseq:
        before_this[ss] -= 1
        if not before_this[ss]:
          games.append(ss)
    
  if num_finished != m:
    ans = -1

print(ans)