Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

game = [[] for i in range(n)]

for i in range(n):
    game[i] = list(map(int,input().split()))

index = [0 for i in range(n)]
days = 0
finish = 0
tot = (n*(n-1))//2
flag = True
ok = True
tempok = False
while flag:
    tempok = False
    cant = set()
    for i in range(n):
        if i in cant:
            continue
        if index[i] >= n-1:
            continue
        vs = game[i][index[i]]
        vs -= 1
        if vs in cant:
            continue
        if game[vs][index[vs]]-1 == i:
            finish += 1
            index[i] += 1
            index[vs] += 1
            tempok = True
            cant.add(vs)
            cant.add(i)
            # print(i,vs)
        if finish >= tot:
            ok = True
            flag = False
            break
    if not tempok:
        ok = False
        break
    days += 1
    # print(finish)
    # print(tot)
    # print(days)
    # print(index)
    # print(game)

if not ok:
    print(-1)
else:
    print(days)


Fixed submission:
def resolve():
    n = int(input())
    a = []

    for i in range(n):
        row = list(map(lambda x: int(x) - 1, input().split()))
        row.reverse()
        a.append(row)

    q = set()

    def check(i):
        if len(a[i]) == 0:
            return
        j = a[i][-1]
        if i == a[j][-1]:
            p = (i, j) if i < j else (j, i)
            q.add(p)

    for i in range(n):
        check(i)

    day = 0
    while len(q) > 0:
        day += 1
        prev_q, q = q, set()

        for p in prev_q:
            (i, j) = p
            a[i].pop()
            a[j].pop()

        for p in prev_q:
            (i, j) = p
            check(i)
            check(j)

    for i in range(n):
        if len(a[i]) != 0:
            print(-1)
            return
    print(day)
    
resolve()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

game = [[] for i in range(n)]

for i in range(n):
    game[i] = list(map(int,input().split()))

index = [0 for i in range(n)]
days = 0
finish = 0
tot = (n*(n-1))//2
flag = True
ok = True
tempok = False
while flag:
    tempok = False
    cant = set()
    for i in range(n):
        if i in cant:
            continue
        if index[i] >= n-1:
            continue
        vs = game[i][index[i]]
        vs -= 1
        if vs in cant:
            continue
        if game[vs][index[vs]]-1 == i:
            finish += 1
            index[i] += 1
            index[vs] += 1
            tempok = True
            cant.add(vs)
            cant.add(i)
            # print(i,vs)
        if finish >= tot:
            ok = True
            flag = False
            break
    if not tempok:
        ok = False
        break
    days += 1
    # print(finish)
    # print(tot)
    # print(days)
    # print(index)
    # print(game)

if not ok:
    print(-1)
else:
    print(days)


Fixed submission:
def resolve():
    n = int(input())
    a = []

    for i in range(n):
        row = list(map(lambda x: int(x) - 1, input().split()))
        row.reverse()
        a.append(row)

    q = set()

    def check(i):
        if len(a[i]) == 0:
            return
        j = a[i][-1]
        if i == a[j][-1]:
            p = (i, j) if i < j else (j, i)
            q.add(p)

    for i in range(n):
        check(i)

    day = 0
    while len(q) > 0:
        day += 1
        prev_q, q = q, set()

        for p in prev_q:
            (i, j) = p
            a[i].pop()
            a[j].pop()

        for p in prev_q:
            (i, j) = p
            check(i)
            check(j)

    for i in range(n):
        if len(a[i]) != 0:
            print(-1)
            return
    print(day)
    
resolve()