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

k,s=map(int,input().split())

count=0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                count +=1
                
print(count)

### FIXED CODE ###

k,s=map(int,input().split())

count=0
for x in range(k+1):
    for y in range(k+1):    
        if s-(x+y) <= k and  s-(x+y) >= 0: 
            count +=1
        
                
print(count)
### BUGGY CODE ###

# DP
N, W = map(int, input().split())

dp = [-1] * (W + 1)
dp[0] = 0
for _ in range(N):
    w, v = map(int, input().split())
    for i in range(W - w, -1, -1):
        if dp[i] != -1:
            if dp[i + w] < dp[i] + v:
                dp[i + w] = dp[i] + v
print(max(dp))


### FIXED CODE ###

n, w = [int(e) for e in input().split()]
d = [[int(e) for e in input().split()] for i in range(n)]
t = {}
t[w] = 0
for i in range(n):
  keys = list(sorted(t.keys()))
  tw, tv = d[i]
  for j in keys:
    nw = j - tw
    if nw < 0:
      continue
    nv = t[j] + tv
    if nw in t:
      if nv > t[nw]:
        t[nw] = nv
    else:
      t[nw] = nv
print(max(t.values()))
### BUGGY CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))

B = [1] * (N+1)
B[1] = A[0]

for i in range(2, N+1):
    B[i] = B[i-1]*A[i-1]

for i in range(K+1, N+1):
    if B[i-1]/B[i-1-K] < B[i]/B[i-K]:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))

for i in range(K, N):
    if A[i - K] < A[i]:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

anum, bnum, time= map(int, input().split(" "))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
tempa = [0]
tempb = [0]
total = 0
for i in range(anum):
    tempa.append(tempa[-1] + a[i])
    if total > time:
        break
for i in range(bnum):
    tempb.append(tempb[-1] + b[i])
    if total > time:
        break
maxi = 0
for i in range(anum + 1):
    for j in range(bnum + 1):
        if tempa[i] + tempb[j] <= time:
            if i + j > maxi:
                maxi = i + j
        else:
            break
print(maxi)

### FIXED CODE ###

import bisect
anum, bnum, time= map(int, input().split(" "))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
tempa = [0]
tempb = [0]
total = 0
for i in range(anum):
    tempa.append(tempa[-1] + a[i])
    if total > time:
        break
for i in range(bnum):
    tempb.append(tempb[-1] + b[i])
    if total > time:
        break
maxi = 0
starta = bisect.bisect_right(tempa, time) - 1
for i in range(starta, -1, -1):
    startb = bisect.bisect_right(tempb, time - tempa[i]) - 1
    if i + startb > maxi:
        maxi = i + startb
print(maxi)
### BUGGY CODE ###

n,m=map(int,input().split())
mod=10**9+7
import math
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
if abs(n-m)>1:
    print(0)
    exit()
print(a)

### FIXED CODE ###

import math

n,m=map(int,input().split())
a=math.factorial(n) * math.factorial(m) 
if abs(n-m) == 0:
    a *= 2
elif abs(n-m) > 1:
    a = 0
print(a%1000000007)
### BUGGY CODE ###

def main():
    number, operations = [int(x) for x in input().split()]
    dists = [int(x) for x in input().split()]

    # number = 635
    # dists = [1] * number
    # operations = number
    satulation = any((
        operations >= 40,
        number <= 3000 and operations >= 25,
        number <= 1000 and operations >= 21,
        number <= 625 and operations >= 20,
    ))
    if satulation:
        print(*([number] * number))
        return

    for xx in range(operations):
        dists_new = [0] * number
        for i, d in enumerate(dists):
            start = max(0, i - d)
            end = min(i + d, number - 1)
            for j in range(start, end + 1):
                dists_new[j] += 1
        dists = dists_new
        if min(dists) == number:
            # すべての光が両端まで届く，変化しなくなる。
            print('Over', xx)
            break

    print(*dists)


if __name__ == '__main__':
    main()


### FIXED CODE ###

from itertools import accumulate


def main():
    number, operations = [int(x) for x in input().split()]
    dists = [int(x) for x in input().split()]

    for ope_count in range(operations):
        temp = [0] * (number + 1)
        for i, v in enumerate(dists):
            left = max(i - v, 0)
            right = min(i + v + 1, number)
            temp[left] += 1
            temp[right] -= 1
        templi = list(accumulate(temp))
        if templi == dists:
            break
        else:
            dists = templi

    print(*list(dists)[:-1])


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

N,M = map(int,input().split())
# AB_s = [list(map(int,input().split())) for _ in range(N)]
dicts = {i:[] for i in range(1,M+1)}
for _ in range(N):
    A,B = map(int,input().split())
    if A in dicts:
        dicts[A].append(B)
answer = 0
wages = []
for i in range(1,M+1):
    wages.extend(dicts[i])
    if wages:
        wages.sort()
        answer += wages.pop()
print(answer)
# AB_s = sorted(AB_s, key=lambda x:-x[0])
# print(AB_s)

### FIXED CODE ###

import heapq
N,M = map(int,input().split())
AB_s = [list(map(int, input().split())) for _ in range(N)]
MAPperA = {i: [] for i in range(1,M+1)}
# 残り日数ごとのマップを作る
for i, AB in enumerate(AB_s):
    if AB[0] in MAPperA:
        MAPperA[AB[0]].append(AB[1])
excutableJobList = []
heapq.heapify(excutableJobList)
answer = 0
# 短期間でできる仕事からリストを作って追加してそーとして最大値を追加していく
for i in range(1,M+1):
    for j in MAPperA[i]:
        heapq.heappush(excutableJobList, j*-1)
    if excutableJobList:
        answer += heapq.heappop(excutableJobList) * -1
print(answer)
### BUGGY CODE ###

input()
a = map(int, input().split())
count = 0
while all(i%2 == 0 for i in a):
  count += 1
print(count)

### FIXED CODE ###

n = int(input())
a = map(int, input().split())
m = float('inf')
for i in a:
  count = 0
  while i%2 == 0:
    i = i//2
    count += 1
  m = min(m, count)
  if m==0:
    break
print(m)
### BUGGY CODE ###

N, K = map(int, input().split())
a = tuple(map(int, input().split()))

def main():
    l = [1 if a[0] >= j else 0 for j in range(K+1)]
    s = a[0]
    for i in range(1,N):
        lt = l
        l = [0] * (K + 1)
        s += a[i]
        l[0] = 1
        t = 1
        for j in range(1, a[i]+1):
                t += lt[j]
                l[j] += t
        for j in range(a[i]+1, min(K+1, s+1)):
                t += (lt[j] - lt[j - a[i] - 1])
                l[j] += t
    return l[-1] % (10 ** 9 + 7)
            
print(main())


### FIXED CODE ###

N, K = map(int, input().split())
a = tuple(map(int, input().split()))

def main():
    l = [1 if a[0] >= j else 0 for j in range(K+1)]
    lt= [0] * (K+1)
    s = a[0]
    for i in range(1,N):
        lt, l = l, lt
        s += a[i]
        l[0] = 1
        t = 1
        for j in range(1, a[i]+1):
                t += lt[j]
                l[j] = t
        for j in range(a[i]+1, min(K+1, s+1)):
                t += (lt[j] - lt[j - a[i] - 1])
                l[j] = t
    return l[-1] % (10 ** 9 + 7)
            
print(main())
### BUGGY CODE ###

#確率は(p_i+1)/2
N, K=(int(i) for i in input().split())
P=[int(i) for i in input().split()]

Q=[(P[i]+1)/2 for i in range(len(P))]
R=[sum(Q[:i]) for i in range(len(Q)+1)]
maxp=0
for i in range(N-K+1):
        maxp=max((R[i+K]-R[i]), maxp)
print(maxp)

### FIXED CODE ###

#確率は(p_i+1)/2
N, K=(int(i) for i in input().split())
P=[int(i) for i in input().split()]

Q=[(P[i]+1)/2 for i in range(len(P))]
R=[0]*(len(Q)+1)
for i in range(1,len(R)):
    R[i] = R[i-1] + Q[i-1]
maxp=0
for i in range(N-K+1):
        maxp=max((R[i+K]-R[i]), maxp)
print(maxp)
### BUGGY CODE ###

N = int(input())
A = input().split()
A = [int(s) for s in A]
i = 0
j = 1
while j <= N:
    if j==A[i]:
        print(i+1)
        i = 0
        j = j + 1
    else:
        i = i + 1


### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
L = [[] for i in range(N)]
for i in range(N):
    L[i] = [A[i], i+1]
L = sorted(L)
for i in range(N):
    print(L[i][1])
### BUGGY CODE ###

import math
n, m = map(int, input().split())
a = list(map(int, input().split()))

for i in range(1, m+1):
    a.sort()
    a[-1] = a[-1] // 2
    if sum(a) == 0:
        break
print(sum(a))

### FIXED CODE ###

import heapq

n, m = map(int, input().split())
a = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(a)

while m > 0:
    a_max = heapq.heappop(a)
    if a_max == 0:
        break
    heapq.heappush(a, (-1)*(-a_max // 2))
    m -= 1
print(sum(a)*(-1))
### BUGGY CODE ###

import copy

N = int(input())
B = [int(i) for i in input().split()]


def dfs(arr, indexes):
    if len(arr) == 0:
        return indexes

    for i in reversed(range(len(arr))):
        if i+1 == arr[i]:
            _indexes = copy.copy(indexes)
            _indexes.append(i)
            res = dfs(arr[:i] + arr[i+1:], _indexes)
            if res:
                return res
    return False


res = dfs(B, [])
if res:
    for i in reversed(res):
        print(i+1)
else:
    print(-1)


### FIXED CODE ###

import copy

N = int(input())
B = [int(i) for i in input().split()]


def dfs(arr, indexes):
    if len(arr) == 0:
        return indexes

    for idx, elm in enumerate(arr):
        if idx+1 < elm:
            return False

    for i in reversed(range(len(arr))):
        if i+1 == arr[i]:
            _indexes = copy.copy(indexes)
            _indexes.append(i)
            res = dfs(arr[:i] + arr[i+1:], _indexes)
            if res:
                return res
    return False


res = dfs(B, [])
if res:
    for i in reversed(res):
        print(i+1)
else:
    print(-1)
### BUGGY CODE ###

N = int(input())
temp = input()
a = [int(i) for i in temp.split()]

num_X = 0

for max_new in range(max(a)+2):
    X_new = 0
    X_new += sum([ 1 for ele in a if ele == max_new ])
    X_new += sum([ 1 for ele in a if ele-1 == max_new ])
    X_new += sum([ 1 for ele in a if ele+1 == max_new ])
    num_X = max(num_X, X_new)

print(num_X)

### FIXED CODE ###

import numpy as np
N = int(input())
temp = input()
a = np.array([int(i) for i in temp.split()])

list_X = [0]*1000000

for ele in a:
    list_X[ele] += 1
    list_X[ele-1] += 1
    list_X[ele+1] += 1

print(max(list_X))
### BUGGY CODE ###

n,p=map(int,input().split())
s=input()
dp=[[-1,-1] for i in range(n)]
for i in range(n):
    for j in range(i,n):
        if int(s[i:j+1])%p==0:
            dp[i]=[1,j+1]
            break

ans=0
for i in range(n):
    if dp[i]!=[-1,-1]:
        ans+=dp[i][0]
        if dp[i][1]!=n:
            if dp[dp[i][1]]!=[-1,-1]:
                dp[dp[i][1]][0]+=dp[i][0]

print(ans)

### FIXED CODE ###

from itertools import groupby
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
    ans=0
    for i in range(n-1,-1,-1):
        #Rとなるもの
        if s[i]%p==0:
            ans+=(i+1)
    exit(print(ans))
S=[0]*(n+1)
po=[1]*(n+1)
for i in range(n):
    po[i+1]=po[i]*10
    po[i+1]%=p
for i in range(n-1,-1,-1):
    S[i]=S[i+1]+s[i]*po[n-1-i]
    S[i]%=p
t=[[S[i],i] for i in range(n+1)]
t.sort()
#[L,R)となるのの候補
u=[[i[1] for i in sorted(list(group))] for key,group in groupby(t,key=lambda x: x[0])]
ans=0
for v in u:
    ans+=(len(v))*(len(v)-1)//2
print(ans)
### BUGGY CODE ###

from collections import deque
n , m , k = map(int,input().split())
tomo = [set() for i in range(n+1)]
bro = [set() for i in range(n+1)]
for i in range(m):
    a , b = map(int,input().split())
    tomo[a].add(b)
    tomo[b].add(a)
for i in range(k):
    c , d = map(int,input().split())
    bro[c].add(d)
    bro[d].add(c)

visit = [True for i in range(n+1)]
kurasuta = []
for i in range(1,n+1):
    if visit[i]:
        kar = {i}
        d = deque()
        d.append(i)
        visit[i] = False
        cou = 1
        while d:
            now = d.popleft()
            for j in tomo[now]:
                if visit[j]:
                    cou += 1
                    visit[j] = False
                    kar.add(j)
                    d.append(j)
        kurasuta.append([kar,cou])
    else:
        continue
ans = [0 for i in range(n)]

for i in range(len(kurasuta)):
    for j in kurasuta[i][0]:
        ans[j-1] = len(kurasuta[i][0]- tomo[j] - bro[j]) - 1

print(*ans)

### FIXED CODE ###

from collections import deque
n , m , k = map(int,input().split())
tomo = [set() for i in range(n+1)]
to = [0 for i in range(n+1)]
bro = [set() for i in range(n+1)]
for i in range(m):
    a , b = map(int,input().split())
    tomo[a].add(b)
    tomo[b].add(a)
    to[a] += 1
    to[b] += 1
for i in range(k):
    c , d = map(int,input().split())
    bro[c].add(d)
    bro[d].add(c)

visit = [True for i in range(n+1)]
kurasuta = []
for i in range(1,n+1):
    if visit[i]:
        kar = {i}
        d = deque()
        d.append(i)
        cou = 1
        visit[i] = False
        while d:
            now = d.popleft()
            for j in tomo[now]:
                if visit[j]:
                    visit[j] = False
                    kar.add(j)
                    d.append(j)
                    cou += 1
        kurasuta.append([kar,cou])
    else:
        continue
ans = [0 for i in range(n)]

for i in range(len(kurasuta)):
    for j in kurasuta[i][0]:
        ans[j-1] = kurasuta[i][1] - to[j] - 1 - len(kurasuta[i][0] & bro[j])

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

def main():
    n,q = map(int, input().split())
    tl = []
    que = []
    d = dict()
    for _ in range(n):
        s,t,x = map(int, input().split())
        tl.append((s-x,1,x))
        tl.append((t-x,0,x))
    for _ in range(q):
        t = int(input())
        que.append(t)
        tl.append((t,2))
    tl.sort()
    wor = dict()
    wcur = 0
    cur = -1
    flg = 0
    for x in tl:
        if x[1] == 1:
            wcur += 1
            wor[x[2]] = 1
            if cur < 0 or x[2] < cur:
                cur = x[2]
                flg = 0
        elif x[1] == 0:
            wcur -= 1 
            wor[x[2]] = 0
            if x[2] == cur:
                flg = 1
            if not wcur:
                cur = -1
                flg = 0
        else:
            if flg:
                cur = min([x for x in wor if wor[x]])
                flg = 0
            d[x[0]] = cur
    for i in que:
        print(d[i])
if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    n,q = map(int, input().split())
    tl = []
    que = []
    d = dict()
    for _ in range(n):
        s,t,x = map(int, input().split())
        tl.append((s-x,1,x))
        tl.append((t-x,0,x))
    for _ in range(q):
        t = int(input())
        tl.append((t,2))
    tl.sort()
    wor = set()
    wcur = 0
    cur = -1
    flg = 0
    for x in tl:
        if x[1] == 1:
            wcur += 1
            wor.add(x[2])
            if cur < 0 or x[2] < cur:
                cur = x[2]
                flg = 0
        elif x[1] == 0:
            wcur -= 1 
            wor.remove(x[2])
            if x[2] == cur:
                flg = 1
            if not wcur:
                cur = -1
                flg = 0
        else:
            if flg:
                cur = min(wor)
                flg = 0
            print(cur)
    
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n = int(input())
A = [i for i in range(1, n + 1)]
B = [n] + [i for i in range(1, n)]
ans = sum(i % j for i, j in zip(B, A))
print(ans)

### FIXED CODE ###

n = int(input())
print((n*(n-1))//2)
### BUGGY CODE ###

import itertools
n,x,y = map(int, input().split())
dist = []
for v in itertools.combinations(range(1,n+1),2):  # sorted
    mins = min(v[1]-v[0], abs(x-v[0])+1+abs(v[1]-y))
    dist.append(min(mins, abs(y-v[0])+1+abs(v[1]-y)))
for i in range(1, n):
    print(dist.count(i))

### FIXED CODE ###

N, X, Y = map(int, input().split())
ans = [0]*(N-1)
for i in range(1, N):
    for j in range(i+1, N+1):
        d = min(j-i, abs(X-i)+abs(j-Y)+1)
        ans[d-1] += 1

print(*ans, sep='\n')
### BUGGY CODE ###

x,k,d=map(int,input().split())

for i in range(k):
  if abs(x-d)<abs(x+d):
    x-=d
  elif abs(x+d)<abs(x-d):
    x+=d
  else:
    x+=d
print(abs(x))

### FIXED CODE ###

x,k,d=map(int,input().split())

x=abs(x)
ans=0
if x>=d*k:
  ans=x-k*d
else:
  e= int(x/d)
  k-=e
  x-=e*d
  if k%2==1:
    x=abs(x-d)
  ans=x

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

from collections import Counter

n = int(input())
aL = [input() for i in range(n)]
count = 0

countL = [Counter(aL[i]).most_common() for i in range(n)] 

for i in range(n):
    for j in range(n):
        if i<j:
            if set(countL[i]) == set(countL[j]):
                count +=1

print(count)

### FIXED CODE ###

from collections import Counter
N = int(input())
S = [input() for i in range(N)]
counter = Counter()
ans = 0
for s in S:
    a = ''.join(sorted(list(s)))
    if a in counter:
        ans += counter[a]
    counter[a] += 1
print(ans)
### BUGGY CODE ###

n, x, m = map(int, input().split())
mod_list = [-1 for i in range(m)]
value = x
i = 0
head = 0
while i < n:
    if value not in mod_list:
        mod_list[i] = value
        i += 1
        value = (value**2)%m
    else:
        head = mod_list.index(value)
        break
if i == n:
    print(sum(mod_list[:n]))
else:
    block_size = i - head
    block_num = (n-head)//block_size
    print(sum(mod_list[:head]) + sum(mod_list[head:i])*block_num + sum(mod_list[head:head+(n-head)%block_size]))

### FIXED CODE ###

n, x, m = map(int, input().split())
mod_value = [-1 for i in range(2*m)] #余りの値を頭から格納
mod_check = [False for i in range(m)] #余りが現れたか否か
value = x
i = 0
head = 0
while i < n:
    if not mod_check[value]:
        mod_value[i] = value
        mod_check[value] = True
        i += 1
        value = (value**2)%m
    else:
        head = mod_value.index(value)
        break
if i == n:
    print(sum(mod_value[:n]))
else:
    block_size = i - head
    block_num = (n-head)//block_size
    print(sum(mod_value[:head]) + sum(mod_value[head:i])*block_num + sum(mod_value[head:head+(n-head)%block_size]))
### BUGGY CODE ###

n,m=map(int,input().split())
A=list(map(int,input().split()))
for i in range(n):
    A[i]=A[i]%m

B=[]

for i in range(n):
    sumi=A[i]
    ri=i+1
    while ri<n and sumi%m!=0 :
        sumi=(sumi+A[ri])%m
        ri+=1
    if ri==n and sumi%m!=0 :
        ri=-1
    B.append(ri)
#print(B)

mco=0
for i in range(n):
    coi=0
    ms=0
    s=i
    while s!=n and s!=-1 :
        ms=s
        s=B[s]
        B[ms]=-1
        coi+=1
        #print("s"+str(s))
    if s==-1 :
        coi-=1
    #print(coi)
    mco+=coi*(coi+1)//2

print(mco)


### FIXED CODE ###

n,m=map(int,input().split())
A=list(map(int,input().split()))

B=[]
su=0
for i in range(n):
    su+=A[i]
    B.append(su)

#print(B)

C={}
C[0]=1
for i in range(n):
    C[B[i]%m]=C.get(B[i]%m,0)+1

#print(C)

#D=C.values()
su=0
#print(C.values())
for i in C.values():
    su+=i*(i-1)//2
print(su)
### BUGGY CODE ###

n,m = map(int,input().split())
a = list(map(int,input().split()))

for i in range(m):
    b,c = map(int,input().split())
    a.extend([c]*b)    

a.sort(reverse=True)
print(sum(a[:n]))

### FIXED CODE ###

n,m = map(int,input().split())
a = list(map(int,input().split()))

bc =[]
for i in range(m):
    b,c = map(int,input().split())
    bc.append((b,c))

bc.sort(key=lambda x:x[1],reverse=True)
newcard=[]
for b,c in bc:
    newcard.extend([c]*b)
    if(len(newcard)>=n):
        break

size = min(n,len(newcard))
a.sort()
for i in range(size):
    if(newcard[i]>a[i]):
        a[i]=newcard[i]
    else:
        break
print(sum(a))
### BUGGY CODE ###

import collections,math
s = input()
t = input()
#tを構成文字で、sにない文字があったらダメ。
#構成できるなら最大でもlen(t)個しかないから、10^100の連結は必要ない
 
#最も不足する文字を探す
ls = collections.Counter(s)
lt = collections.Counter(t)
 
time_dict ={}
tmp=0
max_time=0#一番少ないやつ
for t_key,t_val in lt.items():
    if t_key not in ls:#sの中にtの文字がない
        print(-1)
        exit()
 
#多分TLEするけど、愚直解
cnt = 0
tt = list(t[::-1])
idx=0
while tt!=[]:
    if tt[-1]==s[idx]:
        tt.pop()
    cnt+=1
    idx = (idx+1)%len(s)
print(cnt)

### FIXED CODE ###

import collections,math
s = input()
t = input()
#tを構成文字で、sにない文字があったらダメ。
#構成できるなら最大でもlen(t)個しかないから、10^100の連結は必要ない

#最も不足する文字を探す
ls = collections.Counter(s)
lt = collections.Counter(t)

time_dict ={}
tmp=0
max_time=0#一番少ないやつ
for t_key,t_val in lt.items():
    if t_key not in ls:#sの中にtの文字がない
        print(-1)
        exit()

#多分TLEするかも。。
cnt = 0
tt = list(t[::-1])
idx=0
ss = s
while tt!=[]:
    next_t = tt.pop()
    idx=ss.find(next_t)
    if idx == -1:
        cnt += len(ss)
        idx = s.find(next_t)
        ss = s[idx+1:]
        cnt += idx+1
    else:
        cnt += idx+1
        ss = ss[idx+1:]
print(cnt)
### BUGGY CODE ###

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

id = [0]*N
Lock = [-1]*N
ans = 0
flag = [True]*N
min_id = 0
while(min_id < N-1):
    ans += 1
    if ans > N**2:
        break
    tmp = set()
    for i in range(N):
        if id[i] >= N-1:
            continue
        else:
            if Lock[i] >= 0:
                continue
            else:
                if Lock[A[i][id[i]]-1] == i and i not in tmp:
                    Lock[A[i][id[i]]-1] = -1
                    id[A[i][id[i]]-1] += 1
                    tmp.add(A[i][id[i]]-1)
                    id[i] += 1
                else:
                    if i not in tmp:
                        Lock[i] = A[i][id[i]]-1
    min_id = min(id)
if ans > N**2:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

def main():
    import sys
    import time
    input = sys.stdin.buffer.readline
    start = time.time()
    N = int(input())
    A = [tuple(map(int, input().split())) for i in range(N)]

    id = [0]*N
    Lock = [-1]*N
    ans = 0
    min_id = 0
    M = (N*(N-1)) // 2

    while(min_id < N-1):
        now = time.time()
        if now - start > 1.9:
            ans = M
            break
        ans += 1
        if ans > M:
            break
        tmp = set()
        for i in range(N):
            if id[i] >= N-1:
                continue
            else:
                if Lock[i] >= 0:
                    continue
                else:
                    a = A[i][id[i]]
                    if Lock[a-1] == i and i not in tmp:
                        Lock[a-1] = -1
                        id[a-1] += 1
                        tmp.add(a-1)
                        id[i] += 1
                    else:
                        if i not in tmp:
                            Lock[i] = A[i][id[i]]-1
        min_id = min(id)
    if ans > M:
        print(-1)
    else:
        print(ans)


main()
### BUGGY CODE ###

import itertools
S = str(input())

ans = 0
for i,j in itertools.combinations(range(int(len(S))), 2):
  if int(S[i:j+1]) % 2019 == 0:
    ans += 1
      
print(ans)

### FIXED CODE ###

S = str(input())
li = [0] * 2019
ans = 0
li[0] = 1

d = 1
s = 0
for i in S[::-1]:
  s += int(i) * d
  s = s % 2019
  li[s] += 1
  d *= 10
  d = d % 2019
  
for l in li:
  ans += l * (l-1) // 2

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

import itertools,copy
T = input()
TC = list(copy.deepcopy(T))
N = T.count('?')
B = list(itertools.product(['P','D'], repeat=N))
point = [0]*len(B)
strs = [0]*len(B)
for b,i in zip(B,range(len(B))):
    count = 0
    TC = list(T)
    for j in range(len(T)):
        if TC[j] == '?':
            TC[j] = b[count]
            count += 1
    TC = ''.join(TC)
    strs[i] = TC
    point[i] = TC.count('PD') + TC.count('D')
maxin = max(point)

print(strs[point.index(maxin)])


### FIXED CODE ###

#BBBBBBBBBBB

import sys
import math
import itertools
import collections
import heapq
import numpy as np

rl = sys.stdin.readline

t = rl()
li = []
for i in range(len(t)):
    if t[i] == '?':
        li.append('D')
    else:
        li.append(t[i])
print(''.join(li))
### BUGGY CODE ###

x = int(input())
result = 0
ary = list(map(int,input().split()))
miss = False
i = 1
while(True):
  if (i not in ary):
    if (i == 1):
      miss = True
    break
  b = ary.index(i)
  i += 1
  ary = ary[b+1:]

if miss:
  print(-1)
else:
  print(x - i + 1)


### FIXED CODE ###

x = int(input())
result = 0
ary = list(map(int,input().split()))
miss = False
res = 1
for i in range(x):
  if (res == ary[i]):
    res += 1
print(-1 if res-1 == 0 else x - res + 1)