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

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [A[x][0] for x in range(N)]
    for i in range(N):
        A[i].popleft()

    while c != N * (N - 1):
        day += 1

        nxt2 = copy.copy(nxt)
        tmp = set()
        for i in set(nxt):
            if i == -1 or i in tmp:
                continue
            if i == nxt[nxt[i - 1] - 1]:
                if A[i - 1]:
                    nxt2[i - 1] = A[i - 1].popleft()
                else:
                    nxt2[i - 1] = -1
                if A[nxt[i - 1] - 1]:
                    nxt2[nxt[i - 1] - 1] = A[nxt[i - 1] - 1].popleft()
                else:
                    nxt2[nxt[i - 1] - 1] = -1
                tmp.add(i)
                tmp.add(nxt[i - 1])
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = copy.copy(nxt2)

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import collections
import copy

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [x for x in range(N)]
    oppo = [0] * N

    while c != N * (N - 1):
        day += 1

        for i in nxt:
            oppo[i] = A[i].popleft() if A[i] else -1

        tmp = set()
        for i in nxt:
            if i == -1 or i in tmp:
                continue
            if i == oppo[oppo[i] - 1] - 1:
                tmp.add(i)
                tmp.add(oppo[i] - 1)
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = tmp

    print(day)


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [A[x][0] for x in range(N)]
    for i in range(N):
        A[i].popleft()

    while c != N * (N - 1):
        day += 1

        nxt2 = copy.copy(nxt)
        tmp = set()
        for i in set(nxt):
            if i == -1 or i in tmp:
                continue
            if i == nxt[nxt[i - 1] - 1]:
                if A[i - 1]:
                    nxt2[i - 1] = A[i - 1].popleft()
                else:
                    nxt2[i - 1] = -1
                if A[nxt[i - 1] - 1]:
                    nxt2[nxt[i - 1] - 1] = A[nxt[i - 1] - 1].popleft()
                else:
                    nxt2[nxt[i - 1] - 1] = -1
                tmp.add(i)
                tmp.add(nxt[i - 1])
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = copy.copy(nxt2)

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import collections
import copy

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [x for x in range(N)]
    oppo = [0] * N

    while c != N * (N - 1):
        day += 1

        for i in nxt:
            oppo[i] = A[i].popleft() if A[i] else -1

        tmp = set()
        for i in nxt:
            if i == -1 or i in tmp:
                continue
            if i == oppo[oppo[i] - 1] - 1:
                tmp.add(i)
                tmp.add(oppo[i] - 1)
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = tmp

    print(day)


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    while c != N * (N - 1):
        day += 1
        f = False
        for i in range(N):
            x = float("inf")

            if A[i]:
                x = A[i][0]
                if 0 > x > -day:
                    A[i].popleft()
                    if A[i]:
                        x = A[i][0]
                    else:
                        x = float("inf")

            if x == -day or x == float("inf"):
                continue
            if A[x - 1][0] and 0 > A[x - 1][0] > -day:
                A[x - 1].popleft()
            if A[x - 1] and A[x - 1][0] == i + 1:
                c += 2
                A[i][0] = -day
                A[x - 1][0] = -day
                f = True
        if not f:
            print(-1)
            return

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import collections
import copy

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [x for x in range(N)]
    oppo = [0] * N

    while c != N * (N - 1):
        day += 1

        for i in nxt:
            oppo[i] = A[i].popleft() if A[i] else -1

        tmp = set()
        for i in nxt:
            if i == -1 or i in tmp:
                continue
            if i == oppo[oppo[i] - 1] - 1:
                tmp.add(i)
                tmp.add(oppo[i] - 1)
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = tmp

    print(day)


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    while c != N * (N - 1):
        day += 1
        f = False
        for i in range(N):
            x = float("inf")

            if A[i]:
                x = A[i][0]
                if 0 > x > -day:
                    A[i].popleft()
                    if A[i]:
                        x = A[i][0]
                    else:
                        x = float("inf")

            if x == -day or x == float("inf"):
                continue
            if A[x - 1][0] and 0 > A[x - 1][0] > -day:
                A[x - 1].popleft()
            if A[x - 1] and A[x - 1][0] == i + 1:
                c += 2
                A[i][0] = -day
                A[x - 1][0] = -day
                f = True
        if not f:
            print(-1)
            return

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import collections
import copy

input = sys.stdin.readline


def main():
    N = int(input())
    A = [collections.deque([int(x) for x in input().split()]) for _ in range(N)]

    day = 0
    c = 0
    nxt = [x for x in range(N)]
    oppo = [0] * N

    while c != N * (N - 1):
        day += 1

        for i in nxt:
            oppo[i] = A[i].popleft() if A[i] else -1

        tmp = set()
        for i in nxt:
            if i == -1 or i in tmp:
                continue
            if i == oppo[oppo[i] - 1] - 1:
                tmp.add(i)
                tmp.add(oppo[i] - 1)
                c += 2
        if not tmp:
            print(-1)
            return

        nxt = tmp

    print(day)


if __name__ == '__main__':
    main()