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

input = sys.stdin.readline

n = int(input())

e = [[] for _ in range(n)]
for i in range(n):
    a = map(int, input().split())
    for aa in a:
        aa -= 1
        e[i].append(aa)
# print(e)

inds = [0] * n
cur = [e[i][0] for i in range(n)]
# print(cur)
cnt = 0
while sum(cur) != -n:
    flg = False
    bls = [False] * n
    for ind, target in enumerate(cur):
        if bls[ind] or bls[target]:
            continue
        if cur[target] == ind:
            flg = True
            inds[ind] += 1
            inds[target] += 1
            bls[ind] = True
            bls[target] = True
            cur[ind] = e[ind][inds[ind]] if inds[ind] < n - 1 else -1
            cur[target] = e[target][inds[target]] if inds[target] < n - 1 else -1
    if not flg:
        print(-1)
        exit()
    cnt += 1
    # print(cur, cnt)

print(cnt)


Fixed submission:
def check(player):
    if not a[player]:
        return
    opponent = a[player][-1]
    if not a[opponent]:
        return
    if a[opponent][-1] == player:
        tpl = (player, opponent)
        q.add((min(tpl), max(tpl)))


n = int(input())

a = []
for _ in range(n):
    t = list(map(lambda x: int(x) - 1, input().split()))
    t.reverse()
    a.append(t)

q = set()
for i in range(n):
    check(i)

day = 0
while q:
    day += 1
    prev_q = set()
    q, prev_q = prev_q, q
    for player, opponent in prev_q:
        a[player].pop()
        a[opponent].pop()
    for player, opponent in prev_q:
        check(player)
        check(opponent)

if any(a[player] for player in range(n)):
    print(-1)
    exit()

print(day)

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

input = sys.stdin.readline

n = int(input())

e = [[] for _ in range(n)]
for i in range(n):
    a = map(int, input().split())
    for aa in a:
        aa -= 1
        e[i].append((i, aa))

inds = [0] * n
st = set(e[i][0] for i in range(n))
cnt = 0
while st:
    flg = False
    nst = set()
    for tpl in st.copy():
        x, y = tpl
        if x > y: continue
        if (y, x) in st:
            # print(st, x, y, inds)
            flg = True
            inds[x] += 1
            inds[y] += 1
            st.discard(tpl)
            st.discard((y, x))
            if inds[x] < n - 1:
                nst.add(e[x][inds[x]])
            if inds[y] < n - 1:
                nst.add(e[y][inds[y]])
            # print(nst)
    # print(st, nst, inds, flg)
    if not flg:
        print(-1)
        exit()

    cnt += 1
    st.update(nst)
print(cnt)


Fixed submission:
def check(player):
    if not a[player]:
        return
    opponent = a[player][-1]
    if not a[opponent]:
        return
    if a[opponent][-1] == player:
        tpl = (player, opponent)
        q.add((min(tpl), max(tpl)))


n = int(input())

a = []
for _ in range(n):
    t = list(map(lambda x: int(x) - 1, input().split()))
    t.reverse()
    a.append(t)

q = set()
for i in range(n):
    check(i)

day = 0
while q:
    day += 1
    prev_q = set()
    q, prev_q = prev_q, q
    for player, opponent in prev_q:
        a[player].pop()
        a[opponent].pop()
    for player, opponent in prev_q:
        check(player)
        check(opponent)

if any(a[player] for player in range(n)):
    print(-1)
    exit()

print(day)

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

e = [[] for _ in range(n)]
for i in range(n):
    a = map(int, input().split())
    for aa in a:
        aa -= 1
        e[i].append((i, aa))

inds = [0] * n
st = set(e[i][0] for i in range(n))
cnt = 0
while st:
    flg = False
    nst = set()
    for tpl in st.copy():
        x, y = tpl
        if x > y: continue
        if (y, x) in st:
            # print(st, x, y, inds)
            flg = True
            inds[x] += 1
            inds[y] += 1
            st.discard(tpl)
            st.discard((y, x))
            if inds[x] < n - 1:
                nst.add(e[x][inds[x]])
            if inds[y] < n - 1:
                nst.add(e[y][inds[y]])
            # print(nst)
    # print(st, nst, inds, flg)
    if not flg:
        print(-1)
        exit()

    cnt += 1
    st.update(nst)
print(cnt)


Fixed submission:
def check(player):
    if not a[player]:
        return
    opponent = a[player][-1]
    if not a[opponent]:
        return
    if a[opponent][-1] == player:
        tpl = (player, opponent)
        q.add((min(tpl), max(tpl)))


n = int(input())

a = []
for _ in range(n):
    t = list(map(lambda x: int(x) - 1, input().split()))
    t.reverse()
    a.append(t)

q = set()
for i in range(n):
    check(i)

day = 0
while q:
    day += 1
    prev_q = set()
    q, prev_q = prev_q, q
    for player, opponent in prev_q:
        a[player].pop()
        a[opponent].pop()
    for player, opponent in prev_q:
        check(player)
        check(opponent)

if any(a[player] for player in range(n)):
    print(-1)
    exit()

print(day)