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

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2= i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[(x1, y1)].append((x2, y2))
            in_num[(x2, y2)] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[(i, j)] == 0:
                st.append((i, j))
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        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())
    op = [[]]
    for i in range(N):
        op.append(deque(list(map(int, input().split()))))

    day = 0
    count = (N * (N - 1)) // 2
    can = 1
    while True:
        if count == 0:
            break
        if can == 0:
            day = -1
            break
        played = [0] * (N + 1)
        k = 1
        k_seen = 1
        end_day = 0
        can = 0
        while True:
            if len(op[k]) > 0:
                opponent = op[k][0]
                if len(op[opponent]) > 0:
                    if op[opponent][0] == k:
                        if played[k] == 0 and played[opponent] == 0:
                            count -= 1
                            can = 1
                            op[k].popleft()
                            op[opponent].popleft()
                        played[k] = 1
                        played[opponent] = 1
                        k_seen += 1
                        k = k_seen
                        while played[k] == 1:
                            k += 1
                            if k == N + 1:
                                day += 1
                                end_day = 1
                                break
                    else:
                        played[k] = 1
                        if played[opponent] == 0:
                            k = opponent
                        else:
                            k_seen += 1
                            k = k_seen
                            while played[k] == 1:
                                k += 1
                                if k == N + 1:
                                    day += 1
                                    end_day = 1
                                    break
            else:
                played[k] = 1
                k_seen += 1
                k = k_seen
                while played[k] == 1:
                    k += 1
                    if k == N + 1:
                        day += 1
                        end_day = 1
                        break
            if end_day == 1:
                break

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        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())
    op = [[]]
    for i in range(N):
        op.append(deque(list(map(int, input().split()))))

    day = 0
    ok = 1
    count = (N * (N - 1)) // 2
    can = 1
    while True:
        if not ok:
            day = -1
            print('ok')
            break
        if count == 0:
            break
        if can == 0:
            day = -1
            break
        played = [0] * (N + 1)
        k = 1
        k_seen = 1
        end_day = 0
        can = 0
        while True:
            if len(op[k]) > 0:
                opponent = op[k][0]
                if len(op[opponent]) > 0:
                    if op[opponent][0] == k:
                        if played[k] == 0 and played[opponent] == 0:
                            count -= 1
                            can = 1
                            op[k].popleft()
                            op[opponent].popleft()
                        played[k] = 1
                        played[opponent] = 1
                        k_seen += 1
                        k = k_seen
                        while played[k] == 1:
                            k += 1
                            if k == N + 1:
                                day += 1
                                end_day = 1
                                break
                    else:
                        played[k] = 1
                        if played[opponent] == 0:
                            k = opponent
                        else:
                            k_seen += 1
                            k = k_seen
                            while played[k] == 1:
                                k += 1
                                if k == N + 1:
                                    day += 1
                                    end_day = 1
                                    break
                else:
                    ok = 0
                    break
            else:
                played[k] = 1
                k_seen += 1
                k = k_seen
                while played[k] == 1:
                    k += 1
                    if k == N + 1:
                        day += 1
                        end_day = 1
                        break
            if end_day == 1:
                break

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        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())
    op = [[]]
    for i in range(N):
        op.append(deque(list(map(int, input().split()))))

    day = 0
    ok = 1
    count = (N * (N - 1)) // 2
    can = 1
    while True:
        if not ok:
            day = -1
            print('ok')
            break
        if count == 0:
            break
        if can == 0:
            day = -1
            break
        played = [0] * (N + 1)
        k = 1
        k_seen = 1
        end_day = 0
        can = 0
        while True:
            if len(op[k]) > 0:
                opponent = op[k][0]
                if len(op[opponent]) > 0:
                    if op[opponent][0] == k:
                        if played[k] == 0 and played[opponent] == 0:
                            count -= 1
                            can = 1
                            op[k].popleft()
                            op[opponent].popleft()
                        played[k] = 1
                        played[opponent] = 1
                        k_seen += 1
                        k = k_seen
                        while played[k] == 1:
                            k += 1
                            if k >= N:
                                day += 1
                                end_day = 1
                                break
                    else:
                        played[k] = 1
                        if played[opponent] == 0:
                            k = opponent
                        else:
                            k_seen += 1
                            k = k_seen
                            while played[k] == 1:
                                k += 1
                                if k >= N:
                                    day += 1
                                    end_day = 1
                                    break
                else:
                    ok = 0
                    break
            else:
                played[k] = 1
                k_seen += 1
                k = k_seen
                while played[k] == 1:
                    k += 1
                    if k >= N:
                        day += 1
                        end_day = 1
                        break
            if end_day == 1:
                break

    print(day)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        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())
op = [[]]
for i in range(N):
    op.append(deque(list(map(int, input().split()))))

day = 0
ok = 1
count = (N * (N - 1)) // 2
can = 1
while True:
    if not ok:
        day = -1
        print('ok')
        break
    if count == 0:
        break
    if can == 0:
        day = -1
        break
    played = [0] * (N + 1)
    k = 1
    k_seen = 1
    end_day = 0
    can = 0
    while True:
        if len(op[k]) > 0:
            opponent = op[k][0]
            if len(op[opponent]) > 0:
                if op[opponent][0] == k:
                    if played[k] == 0 and played[opponent] == 0:
                        count -= 1
                        can = 1
                        op[k].popleft()
                        op[opponent].popleft()
                    played[k] = 1
                    played[opponent] = 1
                    k_seen += 1
                    k = k_seen
                    while played[k] == 1:
                        k += 1
                        if k >= N:
                            day += 1
                            end_day = 1
                            break
                else:
                    played[k] = 1
                    if played[opponent] == 0:
                        k = opponent
                    else:
                        k_seen += 1
                        k = k_seen
                        while played[k] == 1:
                            k += 1
                            if k >= N:
                                day += 1
                                end_day = 1
                                break
            else:
                ok = 0
                break
        else:
            played[k] = 1
            k_seen += 1
            k = k_seen
            while played[k] == 1:
                k += 1
                if k >= N:
                    day += 1
                    end_day = 1
                    break
        if end_day == 1:
            break

print(day)


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

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

N = int(input())
op = [[]]
for i in range(N):
    op.append(deque(list(map(int, input().split()))))

day = 0
ok = 1
count = (N * (N - 1)) // 2
can = 1
while True:
    if not ok:
        day = -1
        print('ok')
        break
    if count == 0:
        break
    if can == 0:
        day = -1
        break
    played = [0] * (N + 1)
    k = 1
    end_day = 0
    can = 0
    while True:
        if len(op[k]) > 0:
            opponent = op[k][0]
            if len(op[opponent]) > 0:
                if op[opponent][0] == k:
                    if played[k] == 0 and played[opponent] == 0:
                        count -= 1
                        can = 1
                        op[k].popleft()
                        op[opponent].popleft()
                    played[k] = 1
                    played[opponent] = 1
                    k = 1
                    while played[k] == 1:
                        k += 1
                        if k >= N:
                            day += 1
                            end_day = 1
                            break
                else:
                    played[k] = 1
                    if played[opponent] == 0:
                        k = opponent
                    else:
                        k = 1
                        while played[k] == 1:
                            k += 1
                            if k >= N:
                                day += 1
                                end_day = 1
                                break
            else:
                ok = 0
                break
        else:
            played[k] = 1
            k = 1
            while played[k] == 1:
                k += 1
                if k >= N:
                    day += 1
                    end_day = 1
                    break
        if end_day == 1:
            break

print(day)


Fixed submission:
def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.buffer.readline

    N = int(input())
    A = []
    for _ in range(N):
        A.append(list(map(int, input().split())))

    adj = defaultdict(list)
    in_num = defaultdict(int)
    for i in range(N):
        for j in range(N-2):
            x1 = x2 = i+1
            y1 = A[i][j]
            y2 = A[i][j+1]
            if x1 > y1:
                x1, y1 = y1, x1
            if x2 > y2:
                x2, y2 = y2, x2
            adj[x1*(N+1)+y1].append(x2*(N+1)+y2)
            in_num[x2*(N+1)+y2] += 1
    st = []
    ng = (N * (N - 1)) // 2
    for i in range(1, N+1):
        for j in range(i+1, N+1):
            if in_num[i*(N+1)+j] == 0:
                st.append(i*(N+1)+j)
                ng -= 1
    ans = 0
    while st:
        ans += 1
        st_new = []
        for par in st:
            for child in adj[par]:
                if in_num[child] == 1:
                    st_new.append(child)
                    ng -= 1
                in_num[child] -= 1
        if st_new:
            st = st_new
        else:
            break

    if ng:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()