Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    ans = 0
    while True:
        match = defaultdict(lambda : -1)
        mathced = False
        cleared = True
        for i,a in enumerate(A):
            if len(a) == 0:
                continue
            cleared = False
            if match[a[0]] == i:
                A[a[0]].popleft()
                a.popleft()
                mathced = True
            else:
                match[i] = a[0]
        if not mathced:
            break
        ans += 1
    print(ans if ans > 0 and cleared else -1)




    

    ans = 0

    

    
   
main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    from collections import deque
    A = [deque(map(int,input().split())) for _ in range(N)]
    days = 0


    desired = [ A[i][0] for i in range(N)]
    nextdesired = desired[:]
    while True:
        #print(A)
        
        firstgame = True

        
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        # print(desired)

        # if desired.count(-1) == N:
        #     print(days)
        #     return

        for i in range(N-1):
            
            a = desired[i]
            if a < 1:
                continue
            b = desired[a-1]

            if b == i+1:
                
                if len(A[a-1]) == 1:
                    A[a-1][0] = -1
                else:
                    A[a-1].popleft()
                
                if len(A[i]) == 1:
                    A[i][0] = -1
                else:
                    A[i].popleft()
                    
                desired[a-1] = -1
                nextdesired[i] = A[i][0]
                nextdesired[a-1] = A[a-1][0]

                # idxs[i] += 1
                # if idxs[i] == N-1:
                #     idxs[i] = N-2
                #     A[i][-1] = -1

                # desired[a-1] = -1
                # A[a-1][idxs[a-1]] = -1

                # idxs[a-1] += 1
                # if idxs[a-1] == N-1:
                #     idxs[a-1] = N-2
                #     A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    days += 1

                
        if firstgame:
            if desired.count(-1) == N:
                print(days)
                return
            else:
                print(-1)
                return
        desired = nextdesired[:]

            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    from collections import deque
    A = [deque(map(int,input().split())) for _ in range(N)]
    days = 0

    while True:
        #print(A)
        
        firstgame = True

        desired = [ A[i][0] for i in range(N)]
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        # print(desired)

        # if desired.count(-1) == N:
        #     print(days)
        #     return

        for i in range(N-1):
            
            a = desired[i]
            if a < 1:
                continue
            b = desired[a-1]

            if b == i+1:
                if len(A[a-1]) == 1:
                    A[a-1][0] = -1
                else:
                    A[a-1].popleft()
                
                if len(A[i]) == 1:
                    A[i][0] = -1
                else:
                    A[i].popleft()
                    
                desired[a-1] = -1

                # idxs[i] += 1
                # if idxs[i] == N-1:
                #     idxs[i] = N-2
                #     A[i][-1] = -1

                # desired[a-1] = -1
                # A[a-1][idxs[a-1]] = -1

                # idxs[a-1] += 1
                # if idxs[a-1] == N-1:
                #     idxs[a-1] = N-2
                #     A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    days += 1
        if firstgame:
            if desired.count(-1) == N:
                print(days)
                return
            else:
                print(-1)
                return

            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    from collections import deque
    A = [deque(map(int,input().split())) for _ in range(N)]
    days = 0

    while True:
        #print(A)
        
        firstgame = True
        # desired = [ A[i][idxs[i]] for i in range(N)]
        desired = [ A[i][0] for i in range(N)]
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        # print(desired)

        # if desired.count(-1) == N:
        #     print(days)
        #     return

        for i in range(N-1):
            
            a = desired[i]
            if a < 0:
                continue
            b = desired[a-1]
            # a = A[i][0]
            # if a < 0:
            #     continue
            # b = A[a-1][0]
            # if b < 0:
            #     continue
            # matched
            if b == i+1:
                if len(A[a-1]) == 1:
                    A[a-1][0] = -1
                else:
                    A[a-1].popleft()
                
                if len(A[i]) == 1:
                    A[i][0] = -1
                else:
                    A[i].popleft()
                    
                desired[a-1] = -1

                # idxs[i] += 1
                # if idxs[i] == N-1:
                #     idxs[i] = N-2
                #     A[i][-1] = -1

                # desired[a-1] = -1
                # A[a-1][idxs[a-1]] = -1

                # idxs[a-1] += 1
                # if idxs[a-1] == N-1:
                #     idxs[a-1] = N-2
                #     A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    days += 1
        if firstgame:
            if desired.count(-1) == N:
                print(days)
                return
            else:
                print(-1)
                return

            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    from collections import deque
    A = [deque(map(int,input().split())) for _ in range(N)]
    days = 0

    while True:
        #print(A)
        firstgame = True
        # desired = [ A[i][idxs[i]] for i in range(N)]
        desired = [ A[i][0] for i in range(N)]
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        # print(desired)

        if desired.count(-1) == N:
            print(days)
            return

        for i in range(N):
            
            a = desired[i]
            if a < 0:
                continue
            b = desired[a-1]
            # a = A[i][0]
            # if a < 0:
            #     continue
            # b = A[a-1][0]
            # if b < 0:
            #     continue
            # matched
            if b == i+1:
                if len(A[a-1]) == 1:
                    A[a-1][0] = -1
                else:
                    A[a-1].popleft()
                
                if len(A[i]) == 1:
                    A[i][0] = -1
                else:
                    A[i].popleft()
                    
                desired[a-1] = -1

                # idxs[i] += 1
                # if idxs[i] == N-1:
                #     idxs[i] = N-2
                #     A[i][-1] = -1

                # desired[a-1] = -1
                # A[a-1][idxs[a-1]] = -1

                # idxs[a-1] += 1
                # if idxs[a-1] == N-1:
                #     idxs[a-1] = N-2
                #     A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    days += 1
        if firstgame:
            print(-1)
            return 
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    from collections import deque
    A = [deque(map(int,input().split())) for _ in range(N)]
    days = 0
    idxs = [0] * N
    while True:
        #print(A)
        firstgame = True
        # desired = [ A[i][idxs[i]] for i in range(N)]
        desired = [ A[i][0] for i in range(N)]
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        # print(desired)

        if desired.count(-1) == N:
            print(days)
            return

        for i in range(N):
            
            a = desired[i]
            if a < 0:
                continue
            b = desired[a-1]
            # a = A[i][0]
            # if a < 0:
            #     continue
            # b = A[a-1][0]
            if b < 0:
                continue
            # matched
            if b == i+1:
                A[a-1].popleft()
                if len(A[a-1]) == 0:
                    A[a-1].append(-1)
                A[i].popleft()
                if len(A[i]) == 0:
                    A[i].append(-1)
                desired[a-1] = -1

                # idxs[i] += 1
                # if idxs[i] == N-1:
                #     idxs[i] = N-2
                #     A[i][-1] = -1

                # desired[a-1] = -1
                # A[a-1][idxs[a-1]] = -1

                # idxs[a-1] += 1
                # if idxs[a-1] == N-1:
                #     idxs[a-1] = N-2
                #     A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    days += 1
        if firstgame:
            print(-1)
            return 
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    A = XLMIIS(N)
    days = 0
    idxs = [0] * N
    while True:
        #print(A)
        firstgame = True
        matched = False
        desired = [ A[i][idxs[i]] for i in range(N)]
        # desired = []
        # for i in range(N):
        #     idx = idxs[i]
        #     l = A[i]
        #     #print(N,idx)
        #     desired.append(l[idx])
        #print(desired)

        if desired.count(-1) == N:
            print(days)
            return

        for i in range(N):
            a = desired[i]
            if a < 0:
                continue
            b = desired[a-1]
            if b < 0:
                continue
            # matched
            if b == i+1:
                # A[a-1].pop(0)
                # if len(A[a-1]) == 0:
                #     A[a-1].append(-1)
                # A[i].pop(0)
                # if len(A[i]) == 0:
                #     A[i].append(-1)
                # desired[a-1] = -1
                idxs[i] += 1
                if idxs[i] == N-1:
                    idxs[i] = N-2
                    A[i][-1] = -1

                desired[a-1] = -1
                idxs[a-1] += 1
                if idxs[a-1] == N-1:
                    idxs[a-1] = N -2
                    A[a-1][-1] = -1
                
                if firstgame:
                    firstgame = False
                    matched = True
                    days += 1
        if matched == False:
            print(-1)
            return 
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    A = XLMIIS(N)
    days = 0
    idxs = [0] * N
    while True:
        #print(A)
        firstgame = True
        matched = False
        #desired = [ A[i][idxs[i]] for i in range(N)]
        desired = []
        for i in range(N):
            idx = idxs[i]
            l = A[i]
            #print(N,idx)
            desired.append(l[idx])
        #print(desired)

        if desired.count(-1) == N:
            print(days)
            return

        for i in range(N):
            a = desired[i]
            if a < 0:
                continue
            b = desired[a-1]
            if b < 0:
                continue
            # matched
            if b == i+1:
                # A[a-1].pop(0)
                # if len(A[a-1]) == 0:
                #     A[a-1].append(-1)
                # A[i].pop(0)
                # if len(A[i]) == 0:
                #     A[i].append(-1)
                # desired[a-1] = -1
                idxs[i] += 1
                if idxs[i] == N-1:
                    idxs[i] = N-2
                    A[i][-1] = -1
                desired[a-1] = -1
                idxs[a-1] += 1
                if idxs[a-1] == N-1:
                    idxs[a-1] = N -2
                    A[a-1][-1] = -1
                
                

                if firstgame:
                    firstgame = False
                    matched = True
                    days += 1
        if matched == False:
            print(-1)
            return 
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()

Buggy python submission (verdict: Time Limit Exceeded):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    N = II()
    A = XLMIIS(N)
    days = 0
    while len(A) != 0:
        #print(A)
        firstgame = True
        matched = False
        desired = [ a[0] for a in A]
        if desired.count(-1) == N:
            print(days)
            return
        for i in range(N):
            a = desired[i]
            b = desired[a-1]
            # matched
            if a > 0 and b > 0 and b == i+1:
                A[a-1].pop(0)
                if len(A[a-1]) == 0:
                    A[a-1].append(-1)
                A[i].pop(0)
                if len(A[i]) == 0:
                    A[i].append(-1)
                desired[a-1] = -1
                if firstgame:
                    firstgame = False
                    matched = True
                    days += 1
        if matched == False:
            print(-1)
            return 
            


if __name__ == '__main__':
    main()

Fixed submission:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = []
    for i in range(N):
        A.append(deque(map(lambda x: int(x)-1, input().split())))
    
    dbg('hoge')
    ans = 0
    players = deque(range(N))
    calc = 0
    
    match = defaultdict(lambda : -1)
    first = True
    while True:
        mathced = False
        next_players = deque()
        next_match = defaultdict(lambda : -1)
        # dbg('day',ans)
        for i in players:
            # dbg(i,ans,players,A,match)
            calc += 1
            a = A[i]
            if len(a) == 0:
                continue
            j = a[0]
            b = A[j]

            
            
            if match[j] == i and match[i] == j:
                # dbg(i,j)
                b.popleft()
                # if b[0] != i:
                #     dbg('fuga')
                a.popleft()
                mathced = True
                match[i] = -1
                match[j] = -1
                if len(a) > 0:
                    next_match[i] = a[0]
                    next_players.append(i)

                if len(b) > 0:
                    next_match[j] = b[0]
                    next_players.append(j)


            else:
                next_match[i] = a[0]

        players = next_players
        match.update(next_match)
        if first:
            dbg('hogehoge')
            first = False
            players = range(N)
            continue
        ans += 1
        if not mathced or len(players) == 0:
            break

        
    print(ans if mathced and len(next_players)==0 else -1)
    dbg(calc,mathced,len(next_players),len(a),len(b))







    

    ans = 0

    

    
   
main()