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

n = int(input())
orders = [[x - 1 for x in list(map(int, input().split()))] for _ in range(n)]

d = defaultdict(list)
e = defaultdict(list)

for i in range(n):
	for j in range(n - 2):
		a1, a2 = min(i, orders[i][j]), max(i, orders[i][j])
		b1, b2 = min(i, orders[i][j + 1]), max(i, orders[i][j + 1])
		d[(a1, a2)].append((b1, b2))
		e[(b1, b2)].append((a1, a2))
		if j == n - 3:
			d[(b1, b2)].append((-1, -1))
			e[(-1, -1)].append((b1, b2))

visited = [[0 for _ in range(n)] for _ in range(n)]
cnt_dict = defaultdict(int)

ans = []
def visit(cur):
	a, b = cur
	if visited[a][b] == -1:
		print(-1)
		sys.exit()
	elif visited[a][b] == 0:
		visited[a][b] = -1
		if e[cur]:
			cnt = max([visit(x) for x in e[cur]])
		else:
			cnt = 0
		visited[a][b] = 1
		ans.append(cur)
		cnt_dict[cur] = max(cnt_dict[cur], cnt + 1)
		return cnt + 1
	else:
		return cnt_dict[cur]

print(visit((-1, -1)) - 1)

Fixed submission:
from collections import deque

n = int(input())
orders = [deque([x - 1 for x in list(map(int, input().split()))] + [-1]) for _ in range(n)]

nxt = [orders[i].popleft() for i in range(n)]
days = [0 for _ in range(n)]

q = deque([i for i in range(n)])
while q:
	a = q.popleft()
	b = nxt[a]
	if nxt[nxt[a]] == a:
		days[a], days[b] = [max(days[a], days[b]) + 1 for _ in range(2)]
		nxt[a] = orders[a].popleft()
		nxt[b] = orders[b].popleft()
		q.append(a)
		q.append(b)

if sum(nxt) == -n:
	print(max(days))
else:
	print(-1)

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

n = int(input())
orders = [[x - 1 for x in list(map(int, input().split()))] for _ in range(n)]

d = defaultdict(list)
e = defaultdict(list)

for i in range(n):
	for j in range(n - 2):
		a1, a2 = min(i, orders[i][j]), max(i, orders[i][j])
		b1, b2 = min(i, orders[i][j + 1]), max(i, orders[i][j + 1])
		d[(a1, a2)].append((b1, b2))
		e[(b1, b2)].append((a1, a2))

visited = [[0 for _ in range(n)] for _ in range(n)]
cnt_dict = defaultdict(int)

ans = []
def visit(cur):
	a, b = cur
	if visited[a][b] == -1:
		print(-1)
		sys.exit()
	elif visited[a][b] == 0:
		visited[a][b] = -1
		if e[cur]:
			cnt = max([visit(x) for x in e[cur]])
		else:
			cnt = 0
		visited[a][b] = 1
		ans.append(cur)
		cnt_dict[cur] = max(cnt_dict[cur], cnt + 1)
		return cnt + 1
	else:
		return cnt_dict[cur]

max_cnt = 0
for i in range(n):
	for j in range(i + 1, n):
		if visited[i][j] == 0:
			max_cnt = max(visit((i, j)), max_cnt)
print(max_cnt)

Fixed submission:
from collections import deque

n = int(input())
orders = [deque([x - 1 for x in list(map(int, input().split()))] + [-1]) for _ in range(n)]

nxt = [orders[i].popleft() for i in range(n)]
days = [0 for _ in range(n)]

q = deque([i for i in range(n)])
while q:
	a = q.popleft()
	b = nxt[a]
	if nxt[nxt[a]] == a:
		days[a], days[b] = [max(days[a], days[b]) + 1 for _ in range(2)]
		nxt[a] = orders[a].popleft()
		nxt[b] = orders[b].popleft()
		q.append(a)
		q.append(b)

if sum(nxt) == -n:
	print(max(days))
else:
	print(-1)

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

n = int(input())
orders = [[x - 1 for x in list(map(int, input().split()))] for _ in range(n)]

d = defaultdict(list)
e = defaultdict(list)

for i in range(n):
	for j in range(n - 2):
		a1, a2 = min(i, orders[i][j]), max(i, orders[i][j])
		b1, b2 = min(i, orders[i][j + 1]), max(i, orders[i][j + 1])
		d[(a1, a2)].append((b1, b2))
		e[(b1, b2)].append((a1, a2))

visited = [[0 for _ in range(n)] for _ in range(n)]

tpsort = []
def visit(cur):
	a, b = cur
	if visited[a][b] == -1:
		print(-1)
		sys.exit()
	elif visited[a][b] == 0:
		visited[a][b] = -1
		for nxt in e[cur]:
			visit(nxt)
		visited[a][b] = 1
		tpsort.append(cur)

for i in range(n):
	for j in range(i + 1, n):
		if visited[i][j] == 0:
			#print((i, j))
			visit((i, j))
#print(tpsort)

def dfs(a, cnt):
	if len(d[a]) == 0:
		return cnt
	else:
		return max([dfs(x, cnt + 1) for x in d[a]])

i = 0
max_ans = 0
while True:
	if len(e[tpsort[i]]) > 0:
		break
	max_ans = max(max_ans, dfs(tpsort[i], 1))
	i += 1

print(max_ans)

Fixed submission:
from collections import deque

n = int(input())
orders = [deque([x - 1 for x in list(map(int, input().split()))] + [-1]) for _ in range(n)]

nxt = [orders[i].popleft() for i in range(n)]
days = [0 for _ in range(n)]

q = deque([i for i in range(n)])
while q:
	a = q.popleft()
	b = nxt[a]
	if nxt[nxt[a]] == a:
		days[a], days[b] = [max(days[a], days[b]) + 1 for _ in range(2)]
		nxt[a] = orders[a].popleft()
		nxt[b] = orders[b].popleft()
		q.append(a)
		q.append(b)

if sum(nxt) == -n:
	print(max(days))
else:
	print(-1)

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

n = int(input())	
orders = [deque(list(map(int, input().split()))) for _ in range(n)]

cnt = 0
finished = set()
while True:
	impossible = True
	skip = copy.deepcopy(finished)
	#print(orders)
	for i in range(n):	
		if i not in skip and orders[i][0] - 1 not in skip and orders[orders[i][0] - 1][0] - 1 == i:				
			a = i
			b = orders[i][0] - 1
			impossible = False
			skip.add(a)
			skip.add(b)
			orders[orders[i][0] - 1].popleft()
			orders[i].popleft()

			if len(orders[i]) == 0:
				finished.add(a)
			if len(orders[b]) == 0:
				finished.add(b)
	cnt += 1

	if len(finished) == n:
		print(cnt)
		break

	if impossible:
		print(-1)
		break

Fixed submission:
from collections import deque

n = int(input())
orders = [deque([x - 1 for x in list(map(int, input().split()))] + [-1]) for _ in range(n)]

nxt = [orders[i].popleft() for i in range(n)]
days = [0 for _ in range(n)]

q = deque([i for i in range(n)])
while q:
	a = q.popleft()
	b = nxt[a]
	if nxt[nxt[a]] == a:
		days[a], days[b] = [max(days[a], days[b]) + 1 for _ in range(2)]
		nxt[a] = orders[a].popleft()
		nxt[b] = orders[b].popleft()
		q.append(a)
		q.append(b)

if sum(nxt) == -n:
	print(max(days))
else:
	print(-1)