Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = [list(reversed(list(map(lambda x: int(x) - 1, input().split())))) for _ in range(N)]
    d = 0
    r = N
    cur = list(range(N))
    w = [a.pop() for a in A]
    while r:
        d += 1
        nxt = []
        for i in cur:
            o = w[i]
            if w[o] == i:
                nxt.append(i)
                nxt.append(o)
        if not nxt:
            return -1
        for i in set(nxt):
            if A[i]:
                w[i] = A[i].pop()
            else:
                w[i] = -1
                r -= 1
        cur = nxt
    return d


print(main())


Fixed submission:
def main():
    N = int(input())
    A = [list(reversed(list(map(lambda x: int(x) - 1, input().split())))) for _ in range(N)]
    d = 0
    r = N
    cur = list(range(N))
    w = [a.pop() for a in A]
    while r:
        d += 1
        nxt = []
        for i in cur:
            o = w[i]
            if o == -1:
                continue
            if w[o] == i:
                nxt.append(i)
                nxt.append(o)
                w[i] = -1
                w[o] = -1
        if not nxt:
            return -1
        cur = []
        for i in nxt:
            if A[i]:
                w[i] = A[i].pop()
                cur.append(i)
            else:
                r -= 1
    return d


print(main())

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
    d = 0
    p = [0] * N
    r = N
    cur = list(range(N))
    while r:
        matched = False
        d += 1
        m = [False] * N
        nxt = []
        for i in cur:
            if m[i]:
                continue
            if p[i] == N - 1:
                continue
            o = A[i][p[i]]
            if (not m[o]) and A[o][p[o]] == i:
                matched = True
                p[i] += 1
                p[o] += 1
                m[i] = True
                m[o] = True
                if p[i] == N - 1:
                    r -= 1
                else:
                    nxt.append(i)
                if p[o] == N - 1:
                    r -= 1
                else:
                    nxt.append(o)
        cur = nxt
        if not matched:
            return -1
    return d


print(main())


Fixed submission:
def main():
    N = int(input())
    A = [list(reversed(list(map(lambda x: int(x) - 1, input().split())))) for _ in range(N)]
    d = 0
    r = N
    cur = list(range(N))
    w = [a.pop() for a in A]
    while r:
        d += 1
        nxt = []
        for i in cur:
            o = w[i]
            if o == -1:
                continue
            if w[o] == i:
                nxt.append(i)
                nxt.append(o)
                w[i] = -1
                w[o] = -1
        if not nxt:
            return -1
        cur = []
        for i in nxt:
            if A[i]:
                w[i] = A[i].pop()
                cur.append(i)
            else:
                r -= 1
    return d


print(main())

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    A = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
    d = 0
    p = [0] * N
    r = N
    while r:
        matched = False
        d += 1
        m = [False] * N
        for i in range(N):
            if m[i]:
                continue
            if p[i] == N - 1:
                continue
            o = A[i][p[i]]
            if (not m[o]) and A[o][p[o]] == i:
                matched = True
                p[i] += 1
                p[o] += 1
                m[i] = True
                m[o] = True
                if p[i] == N - 1:
                    r -= 1
                if p[o] == N - 1:
                    r -= 1
        if not matched:
            return -1
    return d


print(main())


Fixed submission:
def main():
    N = int(input())
    A = [list(reversed(list(map(lambda x: int(x) - 1, input().split())))) for _ in range(N)]
    d = 0
    r = N
    cur = list(range(N))
    w = [a.pop() for a in A]
    while r:
        d += 1
        nxt = []
        for i in cur:
            o = w[i]
            if o == -1:
                continue
            if w[o] == i:
                nxt.append(i)
                nxt.append(o)
                w[i] = -1
                w[o] = -1
        if not nxt:
            return -1
        cur = []
        for i in nxt:
            if A[i]:
                w[i] = A[i].pop()
                cur.append(i)
            else:
                r -= 1
    return d


print(main())