Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
A = [None] + [list(map(int, input().split()))[::-1] for _ in range(N)]
ans = 0
while any(A):
    used = set()
    for i in range(1, N+1):
        if not A[i] or i in used:
            continue
        j = A[i][-1]
        if j in used:
            continue
        if A[j][-1] == i:
            used.add(i)
            used.add(j)
            A[i].pop()
            A[j].pop()
    if not used:
        ans = -1
        break
    ans += 1
print(ans)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline

N = int(input())
A = [None] + [deque(map(int, input().split())) for _ in range(N)]
ans = 0
while any(A):
    used = set()
    for i in range(1, N+1):
        if not A[i] or i in used:
            continue
        j = A[i][0]
        if j in used:
            continue
        if A[j][0] == i:
            used.add(i)
            used.add(j)
            A[i].popleft()
            A[j].popleft()
    if not used:
        ans = -1
        break
    ans += 1
print(ans)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    while any(A):
        ok = False
        used = [False] * N
        for i in range(N):
            a = A[i]
            if not a or used[i]:
                continue
            j = a[0]
            b = A[j]
            if not b or used[j]:
                continue
            if i == b[0]:
                ok = True
                A[i].popleft()
                A[j].popleft()
                used[i] = used[j] = True
        if not ok:
            print(-1)
            exit()
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline

N = int(input())
A = [deque(list(map(lambda x: int(x)-1, input().split()))) for _ in range(N)]
ans = 0
while any(A):
    ok = False
    used = [False] * N
    for i in range(N):
        a = A[i]
        if not a or used[i]:
            continue
        j = a[0]
        b = A[j]
        if not b or used[j]:
            continue
        if i == b[0]:
            ok = True
            A[i].popleft()
            A[j].popleft()
            used[i] = True
            used[j] = True
    if not ok:
        print(-1)
        exit()
    ans += 1
print(ans)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N = int(input())
A = [list(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
ans = 0
while any(A):
    ok = False
    used = []
    for i in range(N):
        a = A[i]
        if not a or i in used:
            continue
        j = a[0]
        b = A[j]
        if not b or j in used:
            continue
        if i == b[0]:
            ok = True
            A[i].pop(0)
            A[j].pop(0)
            used.append(i)
            used.append(j)
    if not ok:
        print(-1)
        exit()
    ans += 1
print(ans)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = [deque(map(lambda x: int(x)-1, input().split())) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()