Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

# -*- coding: utf-8 -*-

import sys
from collections import defaultdict, deque

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()

memo = defaultdict(str)
def rec(S):
    if memo[S] != '':
        return memo[S]
    N = len(S)
    res = True
    S = list(S)
    S1 = list(S[0])
    S2 = deque(S[1:])
    for i in range(N-2):
        cur = S2.popleft()
        if S1[-1] != S2[0]:
            res &= rec(''.join(S1+list(S2)))
        S1.append(cur)
    if res:
        memo[''.join(S)] = False
        return False
    else:
        memo[''.join(S)] = True
        return True

if rec(S):
    print('First')
else:
    print('Second')


### FIXED CODE ###

# -*- coding: utf-8 -*-

"""
参考：https://img.atcoder.jp/arc064/editorial.pdf
・最終的には両端にある2種類の文字か、両端が同じ文字ならそれとそれ以外のもう1文字以外は残らない。
・なので残った文字数の偶奇は分かる。
・そこから総ターン数が分かるので、その偶奇で勝敗が分かる。
"""

import sys
from collections import deque, Counter, defaultdict
from math import sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, product, combinations, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial

def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)

if S[0] == S[-1]:
    if N % 2 == 0:
        print('First')
    else:
        print('Second')
else:
    if N % 2 == 0:
        print('Second')
    else:
        print('First')
### BUGGY CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip() 
import re

def resolve():
    N = int(input())
    S = input()

    ans = 0
    for i in range(1000):
        s = str(i).zfill(3)
        p = '.*'+s[0]+'.*'+s[1]+'.*'+s[2]+'.*'
        if re.fullmatch(p, S):
            ans +=1

    print(ans)

if __name__ == '__main__':
    resolve()


### FIXED CODE ###

import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    N = int(input())
    S = input()

    ans = 0
    for i in range(1000):
        s = str(i).zfill(3)
        j = 0
        flg0 = False
        flg1 = False
        flg2 = False
        for j in S:
            if not flg0 and j==s[0]:
                flg0 = True
            elif flg0 and not flg1 and j==s[1]:
                flg1 = True
            elif flg0 and flg1 and not flg2 and j==s[2]:
                flg2 = True
        if flg0 and flg1 and flg2:
            ans +=1

    print(ans)

if __name__ == '__main__':
    resolve()
### BUGGY CODE ###

import sys
input = sys.stdin.readline
S = input().rstrip()
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if len(elem) == 1:
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S = elem[4] + S
    elif s == 2 and flag == 0:
      S += elem[4]
    elif s == 1 and flag == 1:
      S += elem[4]
    else:
      S = elem[4] + S

if flag == 1:
  S = S[::-1]
print(S)

### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import deque

S = input().rstrip()
S = deque(S)
Q = int(input())
Que = (input().rstrip() for _ in range(Q))

flag = 0 #0:そのまま 1:反転
for elem in Que:
  if elem[0] == '1':
    flag ^= 1
  else:
    s = int(elem[2])
    if s == 1 and flag == 0:
      S.appendleft(elem[4])
    elif s == 2 and flag == 0:
      S.append(elem[4])
    elif s == 1 and flag == 1:
      S.append(elem[4])
    else:
      S.appendleft(elem[4])

if flag == 1:
  S.reverse()
print(''.join(S))
### BUGGY CODE ###

from collections import defaultdict


def solve(n, cards):
    dp = {(1 << n) - 1: {0: 0}}
    for d in range(n):
        ndp = defaultdict(lambda: defaultdict(lambda: 1000000))
        for state, scores in dp.items():
            k = state
            curr = 0
            while k:
                b = k & -k
                i = b.bit_length() - 1
                num = cards[i][(d - i) % 2]
                curr_dp = ndp[state ^ b]
                prev_num = 1000000
                for score, left_num in sorted(scores.items()):
                    if left_num > num:
                        continue
                    if prev_num <= left_num:
                        continue
                    curr_dp[score + curr] = min(curr_dp[score + curr], num)
                    prev_num = left_num
                k ^= b
                curr += 1

        if len(ndp) == 0:
            return -1
        dp = ndp
    if 0 in dp and len(dp[0]) > 0:
        return min(dp[0].keys())
    else:
        return -1


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
cards = list(zip(aaa, bbb))
print(solve(n, cards))


### FIXED CODE ###

def iter_bit(k, LIMIT):
    v = (1 << k) - 1

    while v < LIMIT:
        yield v
        x = v & -v
        y = v + x
        v = (v & ~y) // x >> 1 | y


def solve(n, cards):
    dp = [{} for _ in range(1 << n)]
    dp[-1][0] = 0
    LIMIT = 1 << n

    for d in range(n):
        for state in iter_bit(n - d, LIMIT):
            scores = dp[state]
            k = state
            curr = 0
            while k:
                b = k & -k
                i = b.bit_length() - 1
                num = cards[i][(d - i) % 2]
                next_dp = dp[state ^ b]
                for left_num, score in scores.items():
                    if left_num > num:
                        continue
                    ns = score + curr
                    if num not in next_dp or next_dp[num] > ns:
                        next_dp[num] = ns
                k ^= b
                curr += 1

    if len(dp[0]) > 0:
        return min(dp[0].values())
    else:
        return -1


n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
cards = list(zip(aaa, bbb))
print(solve(n, cards))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b=[]

a = a[::-1]
for i in range(n):
    b.append(a.pop(-1))
    b = b[::-1]

for i in range(len(b)):
    print(b[i], end=" ")

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = []

if n % 2 == 0:
    for i in range(n - 1, 0, -2):
        b.append(a[i])
    for i in range(0, n, 2):
        b.append(a[i])
else:
    for i in range(n - 1, -1, -2):
        b.append(a[i])
    for i in range(1, n, 2):
        b.append(a[i])

for i in range(len(b)):
    print(b[i], end=" ")
### BUGGY CODE ###

def small(a, b):
    if a > b:
        return b
    else:
        return a


def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            i_j = i + j
            if i_j < n:
                dp[i_j] = small(dp[i_j], dp[i] + abs(h[i] - h[i_j]))
    print(dp[-1])


if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    INF = 10 ** 9
    dp = [INF] * n
    dp[0] = 0
    for i in range(n):
        for j in range(1, k + 1):
            i_j = i + j
            if i_j < n:
                x = dp[i] + abs(h[i] - h[i_j])
                if dp[i_j] > x:
                    dp[i_j] = x
    print(dp[-1])


if __name__ == "__main__":
    main()
### BUGGY CODE ###

import math
n = int(input())
nn = int(math.sqrt(n))
for i in range(nn,n+1):
    if n%i==0:
        print(len(str(i)))
        break


### FIXED CODE ###

import math
n = int(input())
nn = int(math.sqrt(n))
for i in range(nn,0,-1):
    if n%i==0:
        print(len(str(n//i)))
        break
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)


n,q=map(int,input().split())
ki=[[] for i in range(n+1)]
ki[0]="null"
 
for i in range(n-1):
    a,b=map(int,input().split())
    ki[a].append(b)
    ki[b].append(a)
 
scores=[0 for i in range(n+1)]
scores[0]="null"
for i in range(q):
    p,x=map(int,input().split())
    scores[p]+=x
 
 
ans=[0 for i in range(n+1)]
ans[0]="null"
point=0

ed=[]
 
def tansa(n,point):
    ed.append(n)
    point+=scores[n]
    for p in ki[n]:
        if not p in ed:
            tansa(p,point)
    else:
        ans[n]=point
        
tansa(1,0)
 
ans=ans[1:]
ans=[str(i) for i in ans]
print(" ".join(ans))


### FIXED CODE ###

n,q=map(int,input().split())
L=[[] for i in range(n+1)]
S=[0 for i in range(n+1)]
V=[0 for i in range(n+1)]

for _ in range(n-1):
    a,b=map(int,input().split())
    L[a].append(b)   
    L[b].append(a)

for _ in range(q):
    p,x=map(int,input().split())
    S[p]+=x


que=[1]
head=0
V[1]=1
while len(que)>head:
    now=que[head]
    head+=1
    for nex in L[now]:
        if V[nex]==0:
            V[nex]=1
            que.append(nex)
            S[nex]+=S[now]

print(" ".join([str(i) for i in S[1:]]))
### BUGGY CODE ###

def main():
    from math import ceil
    from bisect import bisect_right
    N, D, A = map(int, input().split())
    Monsters = [list(map(int, input().split())) for i in range(N)]
    Monsters = sorted([[x, ceil(h / A)] for x, h in Monsters])


    class BIT2:
        """ 区間加算BIT(区間加算・区間合計取得) """

        def __init__(self, N):
            self.N = N
            self.data0 = [0] * N
            self.data1 = [0] * N

        def _add(self, data, k, x):
            while k < self.N:
                data[k] += x
                k += k & -k

        def _get(self, data, k):
            s = 0
            while k:
                s += data[k]
                k -= k & -k
            return s

        def add(self, l, r, x):
            """ 区間[l,r)に値xを追加 """

            self._add(self.data0, l, -x*(l-1))
            self._add(self.data0, r, x*(r-1))
            self._add(self.data1, l, x)
            self._add(self.data1, r, -x)

        def query(self, l, r):
            """ 区間[l,r)の和を取得 """

            return self._get(self.data1, r-1) * (r-1) + self._get(self.data0, r-1) \
                 - self._get(self.data1, l-1) * (l-1) - self._get(self.data0, l-1)


    RUQ = BIT2(2 * N)
    for i, (x, h) in enumerate(Monsters, start=1):
        RUQ.add(i, i + 1, h)


    i = 0
    ans = 0
    while i < N:
        cost = RUQ.query(i + 1, i + 2)
        if cost > 0:
            index = bisect_right(Monsters, [Monsters[i][0] + 2 * D, float('inf')])
            RUQ.add(i + 1, index + 1, -cost)
            ans += cost
        i += 1
        #
        # check = []
        # for j in range(1, N + 1):
        #     check.append(str(RUQ.query(j, j + 1)))
        # print(' '.join(check))

    print(ans)


if __name__ == '__main__':
    main()

### FIXED CODE ###

from math import ceil
from collections import deque
N, D, A = map(int, input().split())
Monsters = sorted([list(map(int, input().split())) for i in range(N)])
Monsters = [[x, ceil(h / A)] for x, h in Monsters]


acc_damage = 0
que = deque([])

ans = 0
for x, h in Monsters:
    # 期限切れの蓄積ダメージを削除
    while que and x > que[0][0]:
        limit, damage = que.popleft()
        acc_damage -= damage

    need = max(0, h - acc_damage)
    ans += need
    acc_damage += need

    if need:
        que.append([x + 2 * D, need])

print(ans)
### BUGGY CODE ###

import itertools
import sys

input = sys.stdin.readline

N, K = map(int, input().split())
p = list(map(int, input().split()))
l = [sum(i) for i in itertools.combinations(p, K)]
print(min(l))

### FIXED CODE ###

N, K = map(int, input().split())
p = sorted(list(map(int, input().split())))
print(sum(p[:K]))
### BUGGY CODE ###

def m():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    G = [[[-1, -1] for _ in range(W)] for _ in range(H)]
    for a in range(H):
        left = 0
        for b in range(W):
            if S[a][b] == '#':
                left = 0
            else:
                G[a][b][0] = left + 1
                left = G[a][b][0]
    for b in range(W):
        top = 0
        for a in range(H):
            if S[a][b] == '#':
                top = 0
            else:
                G[a][b][1] = top + 1
                top = G[a][b][1]

    for i in range(H-1, -1, -1):
        right = 0
        for j in range(W-1, -1, -1):
            if G[i][j][0] == -1:
                right = 0
            else:
                G[i][j][0] = max(G[i][j][0], right)
                right = G[i][j][0]

    for j in range(W-1, -1, -1):
        bot = 0
        for i in range(H-1, -1, -1):
            if G[i][j][1] == -1:
                bot = 0
            else:
                G[i][j][1] = max(G[i][j][1], bot)
                bot = G[i][j][1]
    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(sum(G[i][j]), ans)
    return ans-1


print(m())


### FIXED CODE ###

def m():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    Gx = [[-1 for _ in range(W)] for _ in range(H)]
    Gy = [[-1 for _ in range(W)] for _ in range(H)]

    for a in range(H):
        left = 0
        for b in range(W):
            if S[a][b] == '#':
                left = 0
            else:
                Gx[a][b] = left + 1
                left = Gx[a][b]
    for b in range(W):
        top = 0
        for a in range(H):
            if S[a][b] == '#':
                top = 0
            else:
                Gy[a][b] = top + 1
                top = Gy[a][b]

    for i in reversed(range(H)):
        right = 0
        for j in reversed(range(W)):
            if Gx[i][j] == -1:
                right = 0
            else:
                Gx[i][j] = max(Gx[i][j], right)
                right = Gx[i][j]

    for j in reversed(range(W)):
        bot = 0
        for i in reversed(range(H)):
            if Gy[i][j] == -1:
                bot = 0
            else:
                Gy[i][j] = max(Gy[i][j], bot)
                bot = Gy[i][j]
    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(Gx[i][j] + Gy[i][j], ans)
    return ans - 1


print(m())
### BUGGY CODE ###

N = int(raw_input())
L = map(int, raw_input().split())

L.sort()

sum = 0

for i in range(N):
    a = L[i]
    for j in range(i+1,N):
        b = L[j]
        for k in range(j+1,N):
            c = L[k]
            if c >= a+b:
                break
#            print "a,b,c = ", a,b,c                                                                                                                     
            sum = sum + 1

print sum

### FIXED CODE ###

import bisect

def main():
    N = int(input())
    L = list(map(int, input().split()))

    ans = 0
    L = sorted(L)

    for i in range(N):
        a = L[i]
        for j in range(i+1, N):
            b = L[j]
            target = a+b

            c = bisect.bisect_left(L, target)
            ans = ans + (c-j-1)

    print (ans)
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import sys
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
S = 0
MOD = 1000000007

for i in range(N - 1):
    for j in range(i + 1, N):
        S += A[i] ^ A[j]
        S = S % MOD

print(S)

### FIXED CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
MOD = 10 ** 9 + 7
ans = 0

for i in range(61):
    cnt = np.count_nonzero(A & 1)
    ans += (2 ** i) * cnt * (N - cnt)
    A >>= 1

print(ans % MOD)
### BUGGY CODE ###

n = int(input())
ans = []

for i in range(1, n + 1):
  cnt = 0
  for x in range(1, n):
    for y in range(x, n):
      for z in range(y, n):
        if (x + y) ** 2 + (y + z) ** 2 + (z + x) ** 2 == i * 2:
          if (x == y and y != z) or (x != y and y == z):
            cnt += 3
          elif x == y == z:
            cnt += 1
          else:
            cnt += 6
  ans.append(cnt)
for i in range(n):
  print(ans[i])

### FIXED CODE ###

n = int(input())
ans = [0] * n

cnt = 0
for x in range(1, 100):
  for y in range(1, 100):
    for z in range(1, 100):
      f = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
      if f <= n:
        ans[f - 1] += 1
ans.append(cnt)

for j in range(n):
  print(ans[j])
### BUGGY CODE ###

N,M=map(int,input().split())
A=[]
for i in range(N):
    A1=[i for i in map(int,input().split())]
    if i==0:
        A+=[A1]
    else:
        for j in range(len(A)):
            if A[j][0]>A1[0]:
                A.insert(j,A1)
                break
            elif j==i-1:
                A+=[A1]

count=0
price=0
while count<M:
    if A[0][1]>=M-count:
        price+=A[0][0]*(M-count)
        break
    else:
        price+=A[0][0]*A[0][1]
        count+=A[0][1]
    A.pop(0)
    
print(price)

### FIXED CODE ###

import numpy as np
N,M=map(int,input().split())
A=[]
 
for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]

count=0
price=0
for i in range(N):
    if A[i,1]>=M-count:
        price+=A[i,0]*(M-count)
        break
    else:
        price+=A[i,0]*A[i,1]
        count+=A[i,1]

print(price)
### BUGGY CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,t = inpl()
wv = [inpl() for _ in range(n)]
wv.sort(key = lambda x:x[1],reverse = True)
res = 0
for i in range(min(n,10)):
    dp = [[0] * (t+5) for _ in range(n+5)]
    for j in range(n):
        w,v = wv[j]
        for k in range(t):
            if k < w or i == j:
                dp[j+1][k] = dp[j][k]
            else:
                dp[j+1][k] = max(dp[j][k], dp[j][k-w] + v)
    # print(dp[n][t-1])
    res = max(dp[n][t-1] + wv[i][1], res)
print(res)

### FIXED CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,w = inpl()
wv = [inpl() for i in range(n)]
dp = [[0 for i in range(w+1)] for j in range(n+1)]
wv.sort()
wv.sort(key=lambda x:x[1])
ww = [0] * n
vv = [0] * n
for i in range(n):
    ww[i],vv[i] = wv[i][0], wv[i][1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
res = dp[n-1][w-1]+wv[n-1][1]

dp = [[0 for i in range(w+1)] for j in range(n+1)]
ww[-1],ww[-2] = ww[-2],ww[-1]
vv[-1],vv[-2] = vv[-2],vv[-1]
for i in range(n-1):
    for j in range(w+1):
        if j >= ww[i]:
            dp[i+1][j] = max(dp[i][j], dp[i][j-ww[i]]+vv[i])
        else:
            dp[i+1][j] = dp[i][j]
print(max(res,dp[n-1][w-1]+vv[n-1]))
### BUGGY CODE ###

import heapq
N = int(input().strip())
a = [list(map(lambda x: int(x)*(-1), input().split())) for i in range(N)]

for i in range(N):
    tmp=a[:i]+a[i+1:]
    heapq.heapify(tmp)
    ans=heapq.heappop(tmp)[0]*(-1)
    print(ans)

### FIXED CODE ###

N = int(input().strip())
a = [int(input().strip()) for i in range(N)]

a_s=sorted(a,reverse=True)
mx=a_s[0]
for i, val in enumerate(a):
    if val!=mx:
        print(mx)
    else:
        print(a_s[1])
### BUGGY CODE ###


N,W = map(int, input().split())
v = [0] * N
w = [0] * N
dp = [[-1]*(W+1)]*(N+1)

for i in range(N):
    v[i], w[i] = map(int, input().split())

def rec(i,j,dp):
    res = 0
    if dp[i][j] != -1:
        return dp[i][j]
    if i<0:
        return 0
    elif j < w[i]:
        res = rec(i-1, j, dp)
    else:
        res = max(rec(i-1,j,dp),rec(i-1, j-w[i],dp)+v[i])

    return res
print(rec(N-1,W,dp))

### FIXED CODE ###

N,W = map(int, input().split())
dp = [0]*(W+1)

for i in range(1,N+1):
    v,w = map(int,input().split())
    for j in range(W, w-1, -1):
        x = v+dp[j-w]
        if x > dp[j]: dp[j] = x

print(dp[W])
### BUGGY CODE ###

import sys
from collections import deque

n = int(input())

a = []
for i in range(n):
  v = deque(map(int, input().split(' ')))
  a.append(v)

f = True
day = 1
while(f):
  done = []
  nokori_games = 0
  
  for m in range(n):
    nokori_games += len(a[m])
    if m in done:
      pass
    elif len(a[m]):
      teki = a[m][0]
      jibun = m + 1
      if not len(a[teki - 1]):
        print(-1)
        sys.exit()
      elif a[teki - 1][0] == jibun and teki - 1 not in done:
        a[m].popleft()
        a[teki - 1].popleft()
        done.append(m)
        done.append(teki - 1)
        nokori_games -= 1
  if not nokori_games:
    print(day)
    sys.exit()
  else:
    if not len(done):
      print(-1)
      sys.exit()
  day += 1

### FIXED CODE ###

import sys
import time
from collections import deque

start = time.time()

input = sys.stdin.readline

n = int(input())
a = [deque([x for x in list(map(int, input().split()))]) for _ in range(n)]
nokori_games = n * (n - 1) / 2 
day = 1
r5000 = range(5000)
player_in_this_game = set()
rn = range(n)
for _ in r5000:
  for m in rn: #選手ごとに判定
    #整合性のある対戦組み合わせがあれば配列から消す
    #全員の判定が終わって試合が残っていたら1日増やす
    if m in player_in_this_game:
      #自分が既に試合が組まれている
      continue

    la = len(a[m])      
    if la: #選手に試合予定が残っている
      teki = a[m][0] #対戦相手
      jibun = m + 1 #自分
      teki_idx = teki - 1
      if teki_idx in player_in_this_game:
        #相手が既に試合に組まれている
        continue

      if a[teki_idx][0] == jibun: #対戦相手の予定も自分でかつ相手がこのターンで試合中ではない
        a[m].popleft() #自分の予定を消す
        a[teki_idx].popleft() #相手の予定を消す
        player_in_this_game.add(m) #自分を試合中にする
        player_in_this_game.add(teki_idx) #相手を試合中にする
        nokori_games -= 1 #残り試合予定を1つ減らす

  if nokori_games: #残り試合があるのに
    if not len(player_in_this_game): #今回対戦が組まれなければ残りの予定が不整合なのでエラー
      print(-1)
      sys.exit()
  else: #誰も残り試合予定がなければ終了
    print(day)
    sys.exit()
  day += 1
  player_in_this_game.clear()
  elapsed_time = time.time() - start
  if elapsed_time > 1:
    print(n*(n-1)//2)
    exit()