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

sys.setrecursionlimit(10000000)

n = int(sys.stdin.readline().rstrip())
a = [deque(map(int, sys.stdin.readline().split())) for _ in range(n)]

def deepPop(x):
    global a
    global days
    target = a[x][0] -1
    if a[target][0] -1 == x:
        a[target].popleft()
        a[x].popleft()
        maxd = max(days[x] +1, days[target] +1)
        days[x] = maxd
        days[target] = maxd
        if len(a[x]) > 0:
            deepPop(x)
        if len(a[target]) > 0:
            deepPop(target)


day = 0
days = [0] * n
setn = set(range(n))
while True:
    popped = 0
    zeros = 0
    tmpns = list(setn)
    for i in tmpns:
        if len(a[i]) == 0:
            setn.remove(i)
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if a[num][0] - 1 == i:
            deepPop(i)
            popped += 1
    if popped == 0:
        if len(setn) == 0:
            break
        else:
            day = -1
            break

if day == -1:
    print(day)
else:
    print(max(days))


Fixed submission:
import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)

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

sys.setrecursionlimit(10000000)

n = int(sys.stdin.readline().rstrip())
a = [deque(map(int, sys.stdin.readline().split())) for _ in range(n)]

def deepPop(x):
    global a
    global days
    target = a[x][0] -1
    if a[target][0] -1 == x:
        a[target].popleft()
        a[x].popleft()
        maxd = max(days[x] +1, days[target] +1)
        days[x] = maxd
        days[target] = maxd
        if len(a[x]) > 0:
            deepPop(x)
        if len(a[target]) > 0:
            deepPop(target)


day = 0
days = [0] * n
setn = set(range(n))
while True:
    popped = 0
    zeros = 0
    tmpns = copy.copy(setn)
    for i in setn:
        if len(a[i]) == 0:
            tmpns.remove(i)
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if a[num][0] - 1 == i:
            deepPop(i)
            popped += 1
    if popped == 0:
        if len(tmpns) == 0:
            break
        else:
            day = -1
            break
    setn = tmpns

if day == -1:
    print(day)
else:
    print(max(days))


Fixed submission:
import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)

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

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

day = 0
setn = set(range(n))
while True:
    popped = 0
    plist = set()
    zeros = 0
    tmpns = copy.copy(setn)
    for i in setn:
        if len(a[i]) == 0:
            tmpns.remove(i)
            continue
        if i in plist:
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if num in plist:
            continue
        if a[num][0] - 1 == i:
            plist.add(num)
            a[i].popleft()
            a[num].popleft()
            popped += 1
    if popped == 0:
        if len(tmpns) == 0:
            break
        else:
            day = -1
            break
    setn = tmpns
    day += 1

print(day)


Fixed submission:
import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)

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

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

day = 0
setn = set(range(n))
while True:
    popped = 0
    plist = set()
    zeros = 0
    tmpns = copy.copy(setn)
    for i in setn:
        if len(a[i]) == 0:
            tmpns.remove(i)
            continue
        if i in plist:
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if num in plist:
            continue
        if a[num][0] - 1 == i:
            plist.add(num)
            a[i].popleft()
            a[num].popleft()
            popped += 1
    if popped == 0:
        if len(tmpns) == 0:
            break
        else:
            day = -1
            break
    day += 1

print(day)


Fixed submission:
import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)

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

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

day = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    for i in range(n):
        if len(a[i]) == 0:
            zeros += 1
            continue
        if i in plist:
            continue
        num = a[i][0] - 1
        if num < i:
            continue
        if num in plist:
            continue
        if a[num][0] - 1 == i:
            plist.add(num)
            a[i].popleft()
            a[num].popleft()
            popped += 1
    if popped == 0:
        if zeros == n:
            break
        else:
            day = -1
            break
    day += 1

print(day)


Fixed submission:
import sys
from collections import deque

n = int(input())
a = [deque(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]

current = []

for i in range(n):
    target = a[i][0] -1
    if a[target][0] -1 == i and i < target:
        current.append([i, target])

day = 0
zc = 0
while True:
    popped = 0
    plist = set()
    zeros = 0
    tc = []
    for i in current:
        a[i[0]].popleft()
        a[i[1]].popleft()
        if len(a[i[0]]) > 0:
            plist.add(i[0])
        else:
            zc += 1
        if len(a[i[1]]) > 0:
            plist.add(i[1])
        else:
            zc += 1
    for i in plist:
        target = a[i][0] -1
        if a[target][0] -1 == i:
            if not target in plist or i < target:
                tc.append([i, target])
    day += 1
    if len(tc) == 0:
        break
    current = tc

if zc != n:
    day = -1

print(day)