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

N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

canUse = set(range(N))

ans = 0
while any([len(i) > 0 for i in A]):
    # print("\nDay", ans)
    ans += 1
    canUseNew = set()
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            A[first].pop()
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)


Fixed submission:
import sys

N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    # print("LC", len(canUse))
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

class Cycle(Exception):
    pass

paths = {}
st = set()

def dfs(a, b):

    node = (min(a, b), max(a, b))

    if node in paths:
        return paths[node]

    if node in st:
        raise Cycle()

    st.add(node)

    ns = [1]
    i = A[a].index(b)
    for j in range(i):
        ns.append(1 + dfs(a, A[a][j]))

    i = A[b].index(a)
    for j in range(i):
        ns.append(1 + dfs(b, A[b][j]))

    st.remove(node)
    paths[node] = max(ns)
    return paths[node]

ans = -1
try:
    for i in range(N):
        for j in range(N - 1):
            ans = max(ans, dfs(i, A[i][j]))
    print(ans)
except Cycle:
    print(-1)


Fixed submission:
import sys

N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    # print("LC", len(canUse))
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

class Cycle(Exception):
    pass

paths = {}
st = set()

def dfs(a, b):

    node = (min(a, b), max(a, b))

    if node in paths:
        return paths[node]

    if node in st:
        raise Cycle()

    st.add(node)

    ns = [1]
    i = A[a].index(b)
    for j in range(i):
        ns.append(1 + dfs(a, A[a][j]))

    i = A[b].index(a)
    for j in range(i):
        ns.append(1 + dfs(b, A[b][j]))

    st.remove(node)
    paths[node] = max(ns)
    return paths[node]

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)


Fixed submission:
import sys

N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    # print("LC", len(canUse))
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

class Cycle(Exception):
    pass

paths = {}
st = set()

def dfs(a, b):

    if (a, b) in paths:
        return paths[(a, b)]

    if (a, b) in st:
        raise Cycle()

    st.add((a, b))

    ns = [1]
    i = A[a].index(b)
    for j in range(i):
        ns.append(1 + dfs(a, A[a][j]))

    i = A[b].index(a)
    for j in range(i):
        ns.append(1 + dfs(b, A[b][j]))

    st.remove((a, b))
    paths[(a, b)] = max(ns)
    return paths[(a, b)]

ans = -1
try:
    for i in range(N):
        ans = max(ans, dfs(i, A[i][-1]))
    print(ans)
except Cycle:
    print(-1)


Fixed submission:
import sys

N = int(input())
A = [list(map(lambda x: int(x) - 1, input().split(" "))) for i in range(N)]

canUse = set(range(N))

ans = 0
done = 0
while done < N:
    ans += 1
    canUseNew = set()
    # print("LC", len(canUse))
    for n in canUse:
        if not A[n]:
            continue

        first = A[n][-1]
        if first in canUseNew or n in canUseNew:
            continue

        if A[first][-1] == n:
            # print("Do", n + 1, first + 1)
            A[n].pop()
            if len(A[n]) == 0:
                done += 1
            A[first].pop()
            if len(A[first]) == 0:
                done += 1
            canUseNew.add(n)
            canUseNew.add(first)

    if len(canUseNew) == 0:
        print(-1)
        sys.exit(0)
    canUse = canUseNew

print(ans)