Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*(N+1)+y
s = set()
N = int(input())
edge = [[] for _ in range(N*N+N)]
ind = [0]*(N*N+N)
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-1):
        for k in range(j+1,N-1):
            edge[hash(i,A[j])].append(hash(i,A[k]))
            ind[hash(i,A[k])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Fixed submission:
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*(N+1)+y
s = set()
N = int(input())
edge = [[] for _ in range(N*N+N)]
ind = [0]*(N*N+N)
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-2):
            edge[hash(i,A[j])].append(hash(i,A[j+1]))
            ind[hash(i,A[j+1])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*10000+y
edge = defaultdict(list)
ind = defaultdict(int)
s = set()
N = int(input())
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-1):
        for k in range(j+1,N-1):
            edge[hash(i,A[j])].append(hash(i,A[k]))
            ind[hash(i,A[k])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Fixed submission:
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*(N+1)+y
s = set()
N = int(input())
edge = [[] for _ in range(N*N+N)]
ind = [0]*(N*N+N)
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-2):
            edge[hash(i,A[j])].append(hash(i,A[j+1]))
            ind[hash(i,A[j+1])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict,deque
edge = defaultdict(list)
ind = defaultdict(int)
s = set()
N = int(input())
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add((min(i,A[0]),max(i,A[0])))
    for j in range(N-1):
        for k in range(j+1,N-1):
            x1 = min(i,A[j])
            y1 = max(i,A[j])
            x2 = min(i,A[k])
            y2 = max(i,A[k])
            edge[(x1,y1)].append((x2,y2))
            ind[(x2,y2)] += 1
d = deque()
nd = deque()
for x1,y1 in s:
    if ind[(x1,y1)] == 0:
        d.append((x1,y1))
ans = 0
num = 0
while d:
    while d:
        x1,y1 = d.popleft()
        num += 1
        for x2,y2 in edge[(x1,y1)]:
            ind[(x2,y2)] -= 1
            if ind[(x2,y2)] == 0:
                nd.append((x2,y2))
    d = nd
    nd = deque()
    ans += 1
if num == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Fixed submission:
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*(N+1)+y
s = set()
N = int(input())
edge = [[] for _ in range(N*N+N)]
ind = [0]*(N*N+N)
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-2):
            edge[hash(i,A[j])].append(hash(i,A[j+1]))
            ind[hash(i,A[j+1])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from numba import njit

@njit('(i4,i4[:])',cache=True)
def solve(N,A):
    cur = np.zeros(N,np.int32)
    for i in range(N*(N-1)//2):
        already = np.zeros(N,np.int32)
        for j in range(N):
            if already[j]:
                continue
            k = A[j][cur[j]]-1
            if already[k]:
                continue
            if A[k][cur[k]]-1 == j:
                already[j] = 1
                already[k] = 1
                cur[j] += 1
                cur[k] += 1
        for j in range(N):
            if cur[j] != N-1:
                break
        else:
            return i+1
    return -1

N = int(readline())
A = np.zeros((N,N),np.int32)
for i in range(N):
    A[i][:-1] = np.array(readline().split(),np.int32)
    A[i][-1] = -1
print(solve(N,A))

Fixed submission:
from collections import defaultdict,deque
def hash(x,y):
    x,y =min(x,y),max(x,y)
    return x*(N+1)+y
s = set()
N = int(input())
edge = [[] for _ in range(N*N+N)]
ind = [0]*(N*N+N)
for i in range(1,N+1):
    A = list(map(int,input().split()))
    s.add(hash(i,A[0]))
    for j in range(N-2):
            edge[hash(i,A[j])].append(hash(i,A[j+1]))
            ind[hash(i,A[j+1])] += 1
d = deque()
nd = deque()
for hash in s:
    if ind[hash] == 0:
        d.append(hash)
ans = 0
finished = 0
while d:
    while d:
        hash = d.popleft()
        finished += 1
        for nhash in edge[hash]:
            ind[nhash] -= 1
            if ind[nhash] == 0:
                nd.append(nhash)
    d = nd
    nd = deque()
    ans += 1
if finished == N*(N-1)//2:
    print(ans)
else:
    print(-1)