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

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)

Fixed submission:
from collections import deque

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
from collections import deque

N = int(input())
ad = [[] for _ in range(N**2)]
num = [0] * (N**2)
for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        ti, tj = min(i, j), max(i, j)
        nti, ntj = min(i, nj), max(i, nj)
        v = N*ti + tj
        nv = N*nti + ntj
        ad[v].append(nv)
        num[nv] += 1

L = []
S = deque()

for v in range(N**2):
    i, j = divmod(v, N)
    if i >= j:
        continue
    if num[v] == 0:
        S.append(v)

while S:
    v = S.popleft()
    L.append(v)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append(nv)

if len(L) != N*(N-1)//2:
    print(-1)
    exit()

cand_list = []

def dfs(v):
    res = 1
    for nv in ad[v]:
        res = max(res, dfs(nv) + 1)
    return res

ans = dfs(L[0])
print(ans)

Fixed submission:
from collections import deque

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)

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

N = int(input())
A = [deque(int(e)-1 for e in input().split()) for _ in range(N)]

#ad = [[0] * N for _ in range(N)]
game = 0

for d in range(1,N*(N-1)//2+1):
    #print("---------{}日目---------".format(d))
    matched = [0] * N
    for i in range(N):
        if matched[i]:
            continue
        if not A[i]:
            continue
        # 相手の候補
        j = A[i][0]
        if not A[j]:
            continue
        # お互いに最優先で今日中に試合をしていないなら試合をする
        if A[j][0] == i and not matched[j]:
            #print("{}と{}が試合".format(i+1,j+1))
            A[i].popleft()
            A[j].popleft()
            matched[i] = 1
            matched[j] = 1
            #ad[i][j] = 1
            #ad[j][i] = 1
            game += 1
    if game == N*(N-1)//2:
        print(d)
        exit()

print(-1)

Fixed submission:
from collections import deque

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)

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

N = int(input())
A = [deque(int(e)-1 for e in input().split()) for _ in range(N)]

#ad = [[0] * N for _ in range(N)]
game = 0

for d in range(1,N*(N-1)//2+1):
    #print("---------{}日目---------".format(d))
    matched = [0] * N
    for i in range(N):
        if matched[i]:
            continue
        if not A[i]:
            continue
        # 相手の候補
        j = A[i][0]
        if not A[j]:
            continue
        # お互いに最優先で今日中に試合をしていないなら試合をする
        if A[j][0] == i and not matched[j]:
            #print("{}と{}が試合".format(i+1,j+1))
            A[i].popleft()
            A[j].popleft()
            matched[i] = 1
            matched[j] = 1
            #ad[i][j] = 1
            #ad[j][i] = 1
            game += 1
    if game == N*(N-1)//2:
        print(d)
        exit()

print(-1)

Fixed submission:
from collections import deque

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)

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

N = int(input())
A = [deque(int(e)-1 for e in input().split()) for _ in range(N)]

#ad = [[0] * N for _ in range(N)]
game = 0

for d in range(1,N**2+1):
    #print("---------{}日目---------".format(d))
    matched = [0] * N
    for i in range(N):
        if matched[i]:
            continue
        if not A[i]:
            continue
        # 相手の候補
        j = A[i][0]
        if not A[j]:
            continue
        # お互いに最優先で今日中に試合をしていないなら試合をする
        if A[j][0] == i and not matched[j]:
            #print("{}と{}が試合".format(i+1,j+1))
            A[i].popleft()
            A[j].popleft()
            matched[i] = 1
            matched[j] = 1
            #ad[i][j] = 1
            #ad[j][i] = 1
            game += 1
    if game == N*(N-1)//2:
        print(d)
        exit()

print(-1)

Fixed submission:
from collections import deque

N = int(input())
ad = [[] for _ in range(N*N)]
num = [0] * (N*N)

for i in range(N):
    a = [int(e)-1 for e in input().split()]
    for k in range(N-2):
        j = a[k]
        nj = a[k+1]
        v = N*max(i,j) + min(i,j)
        nv = N*max(i,nj) + min(i,nj)
        ad[v].append(nv)
        num[nv] += 1

S = deque()
L = []

for v in range(N*N):
    if num[v] == 0:
        S.append((v,1))

while S:
    v, d = S.popleft()
    L.append(d)
    for nv in ad[v]:
        num[nv] -= 1
        if num[nv] == 0:
            S.append((nv, d+1))

if len(L) != N*N:
    print(-1)
    exit()

ans = max(L)
print(ans)