Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
ans=0
lst=[]
for i in range(n):
    lst.append(deque(a[i]))
    # print(lst[i])
while 1:
    cnt=[0]*n
    ans+=1
    for i in range(n):
        if lst[i]!=deque():
            if cnt[i]==0  and cnt[lst[i][0]-1]==0 and lst[lst[i][0]-1][0]-1==i:
                cnt[i]=1
                cnt[lst[i][0]-1]=1
                lst[lst[i][0]-1].popleft()
                lst[i].popleft()
    # print(ans)
    # print(lst)
    for i in range(n):
        if cnt[i]==1:
            break
    else:
        print(-1)
        quit()
    
    for i in range(n):
        if lst[i]:
            break
    else:
        break
print(ans)

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))

Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define cc(x) cout << (x) << endl
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define repi(i,a,b) for(int i = int(a); i < int(b); ++i)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef vector<long long> vll;

template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

const ll inf = 1LL<<58;
const int iinf = 1<<30;

int main(){
    ll n;
    cin >> n;
    ll now[n][2];
    ll a[n][n-1];
    rep(i,n){
        rep(j,n-1){
            cin >> a[i][j];
        }
    }
    ll end=n*(n-1)/2;
    ll nex=0;
    ll day=0;
    ll sumsiai=0;
    ll siaisuu=0;
    ll minusnex=0;
    while(1){
        day++;
        siaisuu=sumsiai;
        rep(i,n){
            if(now[i][0]==n-1 || now[i][1]== day){
                continue;
            }
            nex=a[i][now[i][0]];
            minusnex=nex-1;
            if(i+1==a[nex-1][now[minusnex][0]] && now[minusnex][1] != day){
                siaisuu+=1;
                now[i][0]+=1;
                now[i][1]=day;
                now[minusnex][1]=day;
                now[minusnex][0]+=1;
            }
        }
        if(siaisuu-sumsiai==0){
            cout << -1;
            break;
        }

    if(siaisuu==end){
        cout << day;
        break;
    }
    sumsiai=siaisuu;
    }
}

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
now=[[0,0] for i in range(n)]
day,sumsiai=0,0
end=n*(n-1)//2
nex,minusnex=0,0
while 1:
    day+=1
    siaisuu=sumsiai
    for i in range(n):
        if now[i][0]==n-1 or now[i][1] == day:
            continue
        nex=a[i][now[i][0]]
        minusnex=nex-1
        if i+1==a[nex-1][now[minusnex][0]] and now[minusnex][1] != day:
            siaisuu+=1
            now[i][0]+=1
            now[i][1],now[minusnex][1]=day,day
            now[minusnex][0]+=1
    if siaisuu-sumsiai==0:
        print(-1)
        quit()
    if siaisuu==end:
        print(day)
        quit()
    sumsiai=siaisuu

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
now=[[0,0] for i in range(n)]
day=0
sumsiai=0
end=n*(n-1)//2
while 1:
    day+=1
    siaisuu=sumsiai
    for i in range(n):
        if now[i][0]==n-1 or now[i][1] == day:
            continue
        nex=a[i][now[i][0]]
        if i+1==a[nex-1][now[nex-1][0]] and now[nex-1][1] != day:
            siaisuu+=1
            now[i][0]+=1
            now[i][1],now[nex-1][1]=day,day
            now[nex-1][0]+=1
    if siaisuu-sumsiai==0:
        print(-1)
        quit()
    if siaisuu==end:
        print(day)
        quit()
    sumsiai=siaisuu

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
now=[[0,0] for i in range(n)]
day=0

while 1:
    day+=1
    siaisuu=0
    for i in range(n):
        if now[i][0]==n-1 or now[i][1] == day:
            continue
        nex=a[i][now[i][0]]
        if i+1==a[nex-1][now[nex-1][0]] and now[nex-1][1] != day:
            siaisuu+=1
            now[i][0]+=1
            now[i][1],now[nex-1][1]=day,day
            now[nex-1][0]+=1
    if siaisuu==0:
        break

for i in range(n):
    if now[i][0]!=n-1:
        print(-1)
        quit()

print(day-1)

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
now=[[0,0] for i in range(n)]
day=0

while 1:
    day+=1
    siaisuu=0
    for i in range(n):
        if now[i][0]==n-1:
            continue
        # print(i)
        nex=a[i][now[i][0]]
        # print(day)
        # print(now)
        # print(nex)
        # print(a[nex-1][now[nex-1]])
        if i+1==a[nex-1][now[nex-1][0]] and now[i][1] != day and now[nex-1][1] != day:
            siaisuu+=1
            now[i][0]+=1
            now[i][1],now[nex-1][1]=day,day
            now[nex-1][0]+=1
    if siaisuu==0:
        break

for i in range(n):
    if now[i][0]!=n-1:
        print(-1)
        quit()

print(day-1)

Fixed submission:
from collections import deque

n=int(input())
a=[list(map(int,input().split())) for i in range(n)]

for i in range(n):
    for j in range(n-1):
        a[i][j]-=1
    a[i].reverse()
# print(a)
day=[0]*n
pair=[-1]*n

q=deque(range(n))

while q:
    me=q.popleft()
    if not a[me]:
        continue
    oppo=a[me].pop()
    if pair[oppo]==me:
        now=max(day[me],day[oppo])+1
        day[me]=now
        day[oppo]=now
        q.append(me)
        q.append(oppo)
    else:
        pair[me]=oppo


for i in range(n):
    if a[i]:
        print(-1)
        break
else:
    print(max(day))