CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(eval(input()))
A = [[-1] + [int(x)-1 for x in row.split()][::-1] for row in sys.stdin.readlines()]

A.append([-1])

def f():
    can_use = []
    for n in range(N):
        if A[A[n][-1]][-1] == n:
            can_use.append(n)
    for n in can_use:
        A[n].pop()
    return len(can_use)

answer = 0
rest = N*(N-1)
while True:
    answer += 1
    x = f()
    if x == 0:
        answer = -1
        break
    rest -= x
    if rest == 0:
        break

print(answer)

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N = int(eval(input()))
A = [[-1] + [int(x)-1 for x in row.split()][::-1] for row in sys.stdin.readlines()]

A.append([-1])

def f(cand):
    cand1 = set()
    can_use = []
    for n in cand:
        if A[A[n][-1]][-1] == n:
            can_use.append(n)
    for n in can_use:
        A[n].pop()
        cand1.add(n)
        if A[n][-1] != -1:
            cand1.add(A[n][-1])
    return len(can_use),cand1

answer = 0
rest = N*(N-1)
cand = set(range(N))
while True:
    answer += 1
    x,cand = f(cand)
    if x == 0:
        answer = -1
        break
    rest -= x
    if rest == 0:
        break

print(answer)
# Original solution
def resolve():
    N = int(eval(input()))
    A = []
    for _ in range(N):
        _a = list([int(x)-1 for x in input().split()])
        A.append(_a)
    ptrs = [0 for _ in range(N)]
    day = 0
    players = set([i for i in range(N)])
    finished = set()
    loops = 0
    while loops < 10**5:
        todays_players = set()
        matched = False
        if len(finished) == N:
            break
        #print("------")
        for home in (players-finished):
            if home in finished:
                continue
            away = A[home][ptrs[home]]
            if away in finished:
                continue
            #print(home, away)
            away_opp = A[away][ptrs[away]]
            if home in todays_players or away in todays_players:
                continue
            if home == away_opp:
                ptrs[home] += 1
                ptrs[away] += 1
                todays_players.add(home)
                todays_players.add(away)
                matched = True
                if ptrs[home] == N-1:
                    finished.add(home)
                if ptrs[away] == N-1:
                    finished.add(away)
        #print(ptrs)
        if matched:
            day += 1
        else:
            day = -1
            break
        loops += 1
    assert loops < 10**5, "error"
    print(day)





if '__main__' == __name__:
    resolve()


# Optimized solution
def resolve():
    N = int(eval(input()))
    A = []
    for _ in range(N):
        _a = list([int(x)-1 for x in input().split()])
        A.append(_a)
    ptrs = [0 for _ in range(N)]
    day = 0
    players = set([i for i in range(N)])
    finished = set()
    yest_players = set([i for i in range(N)])
    while True:
        todays_players = set()
        matched = False
        if len(finished) == N:
            break
        #print("------")
        for home in yest_players:
            if home in finished:
                continue
            away = A[home][ptrs[home]]
            if away in finished:
                continue
            #print(home, away)
            away_opp = A[away][ptrs[away]]
            if home in todays_players or away in todays_players:
                continue
            if home == away_opp:
                ptrs[home] += 1
                ptrs[away] += 1
                todays_players.add(home)
                todays_players.add(away)
                matched = True
                if ptrs[home] == N-1:
                    finished.add(home)
                if ptrs[away] == N-1:
                    finished.add(away)
        yest_players = todays_players
        #print(ptrs)
        if matched:
            day += 1
        else:
            day = -1
            break
    print(day)





if '__main__' == __name__:
    resolve()
# Original solution
#16:07
n = int(eval(input()))
raw = []
for _ in range(n):
  raw.append(list([int(x)-1 for x in input().split()]))
cnt = [0 for _ in range(n)]
now = []
for i in range(n):
  j = raw[i][cnt[i]]
  if i < j and i == raw[j][cnt[j]]:
    now.append(i)
    now.append(j)
ans = 0
while now:
  ans += 1
  last = now
  now = []
  for x in last:
    cnt[x] += 1
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if x == raw[y][cnt[y]]:
        now.append(x)
        now.append(y)
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans)
else:
  print((-1))

# Optimized solution
#TLEの原因は毎回tmpを設定していたこと
#b08はansがとても大きいサンプル
#16:07
n = int(eval(input()))
raw = []
for _ in range(n):
  raw.append(list([int(x)-1 for x in input().split()]))
cnt = [0 for _ in range(n)]
now = []
for i in range(n):
  j = raw[i][cnt[i]]
  if i < j and i == raw[j][cnt[j]]:
    now.append(i)
    now.append(j)
ans = 0
while now:
  ans += 1
  last = now
  now = []
  for x in last:
    cnt[x] += 1
    if cnt[x] < n-1:
      y = raw[x][cnt[x]]
      if x == raw[y][cnt[y]]:
        now.append(x)
        now.append(y)
  #print(now)
if cnt == [n-1 for _ in range(n)]:
  print(ans)
else:
  print((-1))
# Original solution
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [deque([int(x)-1 for x in input().split()]) for _ in range(N)]
    ans = 0
    while any(A):
        ok = False
        used = [False] * N
        for i in range(N):
            a = A[i]
            if not a or used[i]:
                continue
            j = a[0]
            b = A[j]
            if not b or used[j]:
                continue
            if i == b[0]:
                ok = True
                A[i].popleft()
                A[j].popleft()
                used[i] = used[j] = True
        if not ok:
            print((-1))
            exit()
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

# Optimized solution
from collections import deque
import sys
input = sys.stdin.readline


def main():
    N = int(eval(input()))
    A = [deque([int(x)-1 for x in input().split()]) for _ in range(N)]
    ans = 0
    used_prev = set(range(N))
    while any(A):
        used = set()
        for i in used_prev:
            a = A[i]
            if not a or i in used:
                continue
            j = a[0]
            b = A[j]
            if not b or j in used:
                continue
            if i == b[0]:
                A[i].popleft()
                A[j].popleft()
                used.add(i)
                used.add(j)
        if not used:
            ans = -1
            break
        used_prev = used
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
from collections import deque
 
n = int(eval(input()))
lst = [deque(list(map(int, input().split()))) for _ in range(n)]
 
current = []
for i in range(n):
    current.append(lst[i].popleft())
 
count = 0
players = set(range(1, n+1))
while True:
    index_lst = []
    for i in players:
        candidate = current[i-1]
        if candidate == 0:
            continue
        if current[candidate-1] == i:
            index_lst += [i, candidate]   
    if len(index_lst) == 0:
        if sum(current) == 0:
            print(count)
            break
        else:
            print((-1))
            break
    for i in set(index_lst):
        if len(lst[i-1]) == 0:
            current[i-1] = 0
        else:
            current[i-1] = lst[i-1].popleft()
    players = index_lst
    count += 1

# Optimized solution
from collections import deque
 
n = int(eval(input()))
lst = [deque(list(map(int, input().split()))) for _ in range(n)]
 
current = []
for i in range(n):
    current.append(lst[i].popleft())

count = 0
players = set(range(1, n+1))
while True:
    index_lst = []
    for i in players:
        candidate = current[i-1]
        if candidate in index_lst:
            continue
        if current[candidate-1] == i:
            index_lst += [i, candidate]   
    if len(index_lst) == 0:
        if sum(current) == 0:
            print(count)
            break
        else:
            print((-1))
            break
    for i in index_lst:
        if len(lst[i-1]) == 0:
            current[i-1] = 0
        else:
            current[i-1] = lst[i-1].popleft()
    players = set(index_lst)
    count += 1
# Original solution
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(eval(input()))
    A = [[] for _ in range(N)]
    for i in range(N):
        A[i] = list([int(x) - 1 for x in input().split()])

    # print(A)

    days = 0
    ta = [i for i in range(N)]
    while True:
        ta2 = []
        aset = set()
        for i in ta:
            if len(A[i]) == 0 or i in aset:
                continue
            if i == A[A[i][0]][0]:
                ta2.append(i)
                ta2.append(A[i][0])
                aset |= set([i])
                aset |= set([A[i][0]])
        if len(ta2) > 0:
            for t in ta2:
                A[t].pop(0)
            days += 1
            ta = ta2
        else:
            break

    empty = True
    for i in range(N):
        if len(A[i]) != 0:
            empty = False
            break

    if empty:
        print(days)
    else:
        print((-1))


if __name__ == '__main__':
    solve()


# Optimized solution
import sys
from collections import deque
input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(eval(input()))
    A = [deque for _ in range(N)]
    for i in range(N):
        A[i] = deque(list([int(x) - 1 for x in input().split()]))

    days = 0
    ta = [i for i in range(N)]
    while True:
        ta2 = []
        aset = set()
        for i in ta:
            if len(A[i]) == 0 or i in aset:
                continue
            if i == A[A[i][0]][0]:
                ta2.append(i)
                ta2.append(A[i][0])
                aset |= set([i])
                aset |= set([A[i][0]])
        if len(ta2) > 0:
            for t in ta2:
                A[t].popleft()
            days += 1
            ta = ta2
        else:
            break

    empty = True
    for i in range(N):
        if len(A[i]) != 0:
            empty = False
            break

    if empty:
        print(days)
    else:
        print((-1))


if __name__ == '__main__':
    solve()
# Original solution
import sys
input = sys.stdin.readline

from collections import deque

N = int(eval(input()))
A = [deque(list(map(int, input().split()))) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append([x,tmp])
        return

def get_unique_list(seq):
    seen = []
    return [x for x in seq if x not in seen and not seen.append(x)]

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = get_unique_list(q)
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print((-1))
        sys.exit()
print(day)


# Optimized solution
import sys
input = sys.stdin.readline

from collections import deque

N = int(eval(input()))
A = [deque(list(map(int, input().split()))) for _ in range(N)]
q = []
def check(x):
    if len(A[x]) == 0: return
    tmp = A[x][0]-1
    if A[tmp][0]-1 == x:
        if tmp < x: x,tmp=tmp,x
        q.append((x,tmp))
        return

for i in range(N):
    check(i)
day = 0
while len(q) > 0:
    day+=1
    q = list(set(q))
    prevQ=[]
    prevQ,q=q,prevQ
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        A[x].popleft()
        A[y].popleft()
    for i in range(len(prevQ)):
        x,y=prevQ[i]
        check(x)
        check(y)
    
for i in range(N):
    if len(A[i]) != 0:
        print((-1))
        sys.exit()
print(day)
# Original solution
#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
for j in range(n*(n-1)//2):
    check = [1]*n
    flg = False
    flg2 = False
    for i in range(n):
        if len(a[i]) == 0 or check[i] == 0:
            continue
        (x,y) = a[i].popleft()
        flg2 = True
        check[i] = 0
        if a[y-1][0] == (y,x) and check[y-1]:
            a[y-1].popleft()
            flg = True
            check[y-1] = 0
        else:
            a[i].appendleft((x,y))
    if flg:
        ans += 1
    if not flg2:
        print(ans)
        quit()
for i in a:
    if len(i) != 0:
        print((-1))
        quit()
print((n*(n-1)//2))


# Optimized solution
#!/usr/bin/env python3
#ABC139 E

import sys
import math
import bisect
import time
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [deque() for _ in range(n)]
for i in range(1,n+1):
    x = LI()
    for j in x:
        a[i-1].append((i,j))
ans = 0
s = time.time()
for j in range(n*(n-1)//2):
    check = [1]*n
    flg = False
    flg2 = False
    if time.time() - s > 1.6:
        print((n*(n-1)//2))
        quit()
    for i in range(n):
        if len(a[i]) == 0 or check[i] == 0:
            continue
        (x,y) = a[i].popleft()
        flg2 = True
        check[i] = 0
        if a[y-1][0] == (y,x) and check[y-1]:
            a[y-1].popleft()
            flg = True
            check[y-1] = 0
        else:
            a[i].appendleft((x,y))
    if flg:
        ans += 1
    if not flg2:
        print(ans)
        quit()
for i in a:
    if len(i) != 0:
        print((-1))
        quit()
print(ans)
# Original solution
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N = int( eval(input()))
    A = [ deque( list(map( int, input().split()))) for _ in range(N)]
    cnt = N*(N-1)
    days = 0
    W = [False]*N
    while cnt > 0:
        days += 1
        now = cnt
        V = [0]*N
        for i in range(N):
            if V[i] == 1 or W[i]:
                continue
            a = A[i][0]
            a -= 1
            if V[a] == 1 or W[a]:
                continue
            if i+1 == A[a][0]:
                V[a] = 1
                V[i] = 1
                A[a].popleft()
                A[i].popleft()
                if not A[a]:
                    W[a] = True
                if not A[i]:
                    W[i] = True
                cnt -= 2
        if now == cnt:
            print((-1))
            return
    print( days)

                        
if __name__ == '__main__':
    main()


# Optimized solution
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N = int( eval(input()))
    A = [deque()] + [deque( list(map( int, input().split()))) for _ in range(N)]
    Days = [0]*(N+1)
    NOW = [0]*(N+1)
    # for i in range(1, N+1):
    #     a = A[i].popleft()
    #     if NOW[a] == i:
    #         Days[a] = Days[i] = max(Days[a], Days[i])
    q = deque( [ i+1 for i in range(N)])
    while q:
        i = q.popleft()
        if not A[i]:
            continue
        a = A[i].popleft()
        if NOW[a] == i:
            Days[a] = Days[i] = max(Days[a], Days[i])+1
            q.append(i)
            q.append(a)
            NOW[a] = NOW[i] = 0
        else:
            NOW[i] = a
    for i in range(1,N+1):
        if A[i]:
            print((-1))
            return
    print(( max(Days)))
if __name__ == '__main__':
    main()
# Original solution
import sys

N=int(eval(input()))
amat=[[-1]]
for i in range(N):
  alist=list(map(int,input().split()))
  amat.append(list(reversed(alist)))
  
#print(amat)
answer=0
matched_flag=[False]*(N+1)
for answer in range(1,N**3):
  if N>900 and answer>5000:
    print((N*(N-1)//2))
    sys.exit()
  
  matched_flag=[False]*(N+1)
  unchanged=True
  
  for i in range(1,N+1):
    if matched_flag[i] or len(amat[i]) == 0:
      continue
      
    ai_next = amat[i][-1]
    if amat[ai_next][-1]==i and not matched_flag[ai_next]:
      #print(i,ai_next,answer)
      matched_flag[amat[i].pop()]=True
      matched_flag[amat[ai_next].pop()]=True
      unchanged=False
  
  if unchanged:
    print((-1))
    sys.exit()
  else:
    for i in range(1,N+1):
      if len(amat[i])>0:
        break
    else:
      print(answer)
      sys.exit()


# Optimized solution
import sys

N=int(eval(input()))
amat=[[-1]]
for i in range(N):
  alist=list(map(int,input().split()))
  amat.append(list(reversed(alist)))
  
#print(amat)
answer=0
matched_flag=[False]*(N+1)
for answer in range(1,N**3):
  if N>900 and answer>3000:
    print((N*(N-1)//2))
    sys.exit()
  
  matched_flag=[False]*(N+1)
  unchanged=True
  
  for i in range(1,N+1):
    if matched_flag[i] or len(amat[i]) == 0:
      continue
      
    ai_next = amat[i][-1]
    if amat[ai_next][-1]==i and not matched_flag[ai_next]:
      #print(i,ai_next,answer)
      matched_flag[amat[i].pop()]=True
      matched_flag[amat[ai_next].pop()]=True
      unchanged=False
  
  if unchanged:
    print((-1))
    sys.exit()
  else:
    for i in range(1,N+1):
      if len(amat[i])>0:
        break
    else:
      print(answer)
      sys.exit()
# Original solution
import sys

N=int(eval(input()))
amat=[[-1]]
for i in range(N):
  alist=list(map(int,input().split()))
  amat.append(list(reversed(alist)))
  
#print(amat)
cleared_num=0
new_search_index=set(range(1,N+1))
for answer in range(1,N**3):
  matched_flag=[False]*(N+1)
  unchanged=True

  search_index=new_search_index;
  new_search_index=set()
  for i in search_index:
    if matched_flag[i]:
      continue
      
    ai_next = amat[i][-1]
    if amat[ai_next][-1]==i and not matched_flag[ai_next]:
      #print(i,ai_next,answer)
      matched_flag[amat[i].pop()]=True
      matched_flag[amat[ai_next].pop()]=True
      if len(amat[i])==0:
        cleared_num+=1
      else:
        new_search_index.add(i)  
      if len(amat[ai_next])==0:
        cleared_num+=1
      else:
        new_search_index.add(ai_next)
      unchanged=False
      
  if unchanged:
    print((-1))
    sys.exit()
  elif cleared_num==N:
    print(answer)
    sys.exit()

# Optimized solution
import sys

N=int(eval(input()))
amat=[[-1]]
for i in range(N):
  alist=list(map(int,input().split()))
  amat.append(list(reversed(alist)))
  
#print(amat)
cleared_num=0
new_search_index=set(range(1,N+1))
for answer in range(1,N**3):
  matched_set=set()
  unchanged=True

  search_index=new_search_index;
  new_search_index=set()
  for i in search_index:
    if i in matched_set:
      continue
      
    ai_next = amat[i][-1]
    if amat[ai_next][-1]==i and not ai_next in matched_set:
      #print(i,ai_next,answer)
      matched_set.add(amat[i].pop())
      matched_set.add(amat[ai_next].pop())
      if len(amat[i])==0:
        cleared_num+=1
      else:
        new_search_index.add(i)  
      if len(amat[ai_next])==0:
        cleared_num+=1
      else:
        new_search_index.add(ai_next)
      unchanged=False
      
  if unchanged:
    print((-1))
    sys.exit()
  elif cleared_num==N:
    print(answer)
    sys.exit()
# Original solution
import sys
from collections import deque

input = sys.stdin.readline
sys.setrecursionlimit(1100000)
N = int(eval(input()))
side = [[[]for _ in range(N)]for _ in range(N)] 
node = [[1]*N for i in range(N)]
for i in range(N):
    A = list(map(int,input().split()))
    for j in range(N-2):
        a,b = min(i,A[j]-1),max(i,A[j]-1)
        c,d = min(i,A[j+1]-1),max(i,A[j+1]-1)
        side[a][b].append([c,d])
        node[c][d] = 0
def DFS(x,y,l):
    if l > N**2:
        print((-1))
        exit()
    ret = l
    for i,j in side[x][y]:
        ret = max(ret, DFS(i,j,l+1))
    return ret


ans = 0
for i in range(N):
    for j in range(i+1,N):
        if node[i][j]:
            ans = max(DFS(i,j,1),ans)
if ans:
    print(ans)
else:
    print((-1))


# Optimized solution
N = int(eval(input()))
A = list(list(map(int,input().split()))for _ in range(N))

c = [0]*(N)
p = []
for i in range(N):
    j = A[i][c[i]]-1
    if i == A[j][c[j]]-1 and i < j:
        p.append((i,j))
ans = 0
while p:
    q = []
    for i,j in p:
        c[i] += 1
        c[j] += 1
        if c[i] < N-1:
            k = A[i][c[i]]-1
            if A[k][c[k]]-1 == i:
                q.append((i,k))
        if c[j] < N-1:
            k = A[j][c[j]]-1
            if A[k][c[k]]-1 == j:
                q.append((j,k))
    ans += 1
    p = q[:]

if min(c) != N-1:
    print((-1))
else:
    print(ans)
# Original solution
n = int(eval(input()))
a = [0] * (n+1)
for i in range(n):
    a[i+1] = tuple(map(int, input().split()))
from queue import Queue
todo = [0] * (n+1)
matched = Queue()
for i in range(1, n+1):
    matched.put(i)
days = 0
while True:
    update = []
    while matched.empty() == False:
        i = matched.get()
        if todo[i] < n - 1 and i not in update:
            p = a[i][todo[i]]
            if a[p][todo[p]] == i:
                update.append(i)
                update.append(p)
    for j in update:
        todo[j] += 1
        matched.put(j)
    days += 1
    #print(todo)
    if update == []:
        days = -1
        break
    elif todo[1:] == [n - 1] * n:
        break
print(days)


# Optimized solution
n = int(eval(input()))
a = []
for _ in range(n+1):
    a.append([-1])
for i in range(n):
    inp = list(map(int, input().split()))
    for j in range(n-2, -1, -1):
        a[i+1].append(inp[j])
matched = [i for i in range(1, n+1)]
finish = 0
days = 0
while True:
    used = set()
    while matched != []:
        i = matched.pop()
        p = a[i][-1]
        if p != -1 and a[p][-1] == i and (i not in used) and (p not in used) :
            a[i].pop()
            a[p].pop()
            used.add(i)
            used.add(p)
            if a[i][-1] == -1:
                finish += 1
            if a[p][-1] == -1:
                finish += 1
    days += 1
    matched = list(used)
    if len(used) == 0:
        days = -1
        break
    elif finish == n:
        break
print(days)
# Original solution
import sys, time
def input():
	return sys.stdin.readline()[:-1]

n = int(eval(input()))
a = [list(map(int, input().split()))[::-1] for _ in range(n)]
#print(*a, sep="\n")
opponent = [-1 for _ in range(n)]
played = list(range(n))
match = 0
day = 0
while match < n*(n-1)//2:
	approved = False
	for i in played:
		if a[i] != []:
			opponent[i] = a[i].pop()-1
	played = []
	#print(opponent)
	for i in range(n):
		if opponent[i] != -1 and opponent[opponent[i]] == i:
			approved = True
			match += 1
			played += [i, opponent[i]]
			opponent[opponent[i]] = -1
			opponent[i] = -1
	#print(opponent)
	if not approved:
		print((-1))
		sys.exit()
	day += 1
print(day)

# Optimized solution
import sys, time
def input():
	return sys.stdin.readline()[:-1]

n = int(eval(input()))
a = [list(map(int, input().split()))[::-1] for _ in range(n)]
#print(*a, sep="\n")
opponent = [-1 for _ in range(n)]
played = list(range(n))
match = 0
day = 0
while match < n*(n-1)//2:
	approved = False
	for i in played:
		if a[i] != []:
			opponent[i] = a[i].pop()-1
	played_next = []
	#print(opponent)
	for i in played:
		if opponent[i] != -1 and opponent[opponent[i]] == i:
			approved = True
			match += 1
			played_next += [i, opponent[i]]
			opponent[opponent[i]] = -1
			opponent[i] = -1
	#print(opponent)
	if not approved:
		print((-1))
		sys.exit()
	day += 1
	played = played_next
print(day)