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

from collections import deque

s = input()
q = int(input())
query = [input().split() for i in range(q)]

d = deque(['S'])

#d = ['S']

rev = 0

for i in range(q):
    if query[i][0] == '2':
        if query[i][1] == '1':
            d.appendleft(query[i][2])
        else:
            d.append(query[i][2])
    else:
        d.reverse()
        rev += 1

#ans = deque()

if rev % 2 == 1:
    s = ''.join(list(reversed(s)))

for i in range(len(d)):
    if d[i] == 'S':
        d[i] = s

print(*d,sep='')

### FIXED CODE ###

from collections import deque

s = input()
q = int(input())
query = [input().split() for i in range(q)]

d = deque(['S'])

#d = ['S']

rev = False

for i in range(q):
    if query[i][0] == '1':
        rev = not rev
        continue
    
    if not rev:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.appendleft(query[i][2])
            else:
                d.append(query[i][2])
    else:
        if query[i][0] == '2':
            if query[i][1] == '1':
                d.append(query[i][2])
            else:
                d.appendleft(query[i][2])

#ans = deque()
if rev:
    #d = ''.join(list(reversed(d)))
    s = s[::-1]



while len(d):
    if rev:
        tmp = d.pop()
    else:
        tmp = d.popleft()
    if tmp == 'S':
        #ans.append(s)
        print(s,end='')
    else:
        print(tmp,end='')
        #ans.append(tmp)

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

X,Y,Z,K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []

for j in range(X):
    flag2 = 0
    for k in range(Y):
        flag = 0
        for l in range(Z):
            if j*k*l+1 <= K:
                ans.append(A[j]+B[k]+C[l])
            else: 
                if l == 0:
                    flag = 1
                break
        if k == 0 and flag == 1:
            flag2 = 1
            break
    if flag2 == 1:
        break


ans.sort(reverse=True)
for i in range(K):
    print(ans[i])

### FIXED CODE ###

X,Y,Z,K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ans = []

for j in range(X):
    if j+1 > K:
        break
    for k in range(Y):
        if j*k+1 > K:
            break
        for l in range(Z):
            if j*k*l+1 <= K:
                ans.append(A[j]+B[k]+C[l])
            else: 
                break
                
ans.sort(reverse=True)
for i in range(K):
    print(ans[i])
### BUGGY CODE ###

#!/usr/bin/env pypy
n = int(input())
F = [1] * -~n
for i in range(2, n + 1):
    for j in range(i, n + 1, i):
        F[j] += 1
print(sum(e * v for e, v in enumerate(F)))

### FIXED CODE ###

n = int(input())
print(sum(n//i * (i + n//i*i)//2 for i in range(1, n + 1)))
### BUGGY CODE ###

n = int(input())
sl = []
for _ in range(n):
    sl.append(input())
rl=[]
ll=[]
rslt=0
for i in range(n):
    r=sorted(sl[i])
    if r in rl: ll[rl.index(r)]+=1; rslt+=ll[rl.index(r)]
    else: rl.append(r); ll.append(0)
print(rslt)


### FIXED CODE ###

n = int(input())
S=[input() for _ in range(n)]
S=[''.join(sorted(i)) for i in S]
d={}
rslt=0
for s in S:
    if s in d:
        rslt+=d[s]
        d[s]+=1
    else:
        d[s]=1
print(rslt)
### BUGGY CODE ###

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

data = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    a,b = map(int,input().split())
    data[a-1][b-1],data[b-1][a-1] = 1,1

#print(data)
ans = 0
for i in range(n):
    ca = 1
    for k,j in enumerate(data[i]):
        if j == 1:
            if h[i]<=h[k]:
                ca = 0
    ans += ca

print(ans)

### FIXED CODE ###

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

data = [0]*n
#print(h)
#print(data)
for i in range(m):
    a,b = map(int,input().split())
    k = max(data[a-1],h[b-1])
    l = max(h[a-1],data[b-1])
    data[a-1],data[b-1] =k,l

ans = 0

#print(data)
for i in range(n):
    if h[i] > data[i]:
        ans += 1

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

h,w,m=map(int,input().split())
xnum=[[0,i] for i in range(h)]
ynum=[[0,i] for i in range(w)]
hw=[]
xmax=[]
ymax=[]
for i in range(m):
  s,t=map(int,input().split())
  xnum[s-1][0]+=1
  ynum[t-1][0]+=1
  hw.append([s-1,t-1])
xnum.sort()
ynum.sort()
xmax.append(xnum[-1][1])
ymax.append(ynum[-1][1])
for i in range(2,len(xnum)+1):
  if xnum[-1*i][0]==xnum[-1][0]:
    xmax.append(xnum[-1*i][1])
  else:
    break
for i in range(2,len(ynum)+1):
  if ynum[-1*i][0]==ynum[-1][0]:
    ymax.append(ynum[-1*i][1])
  else:
    break
flag=True
for i in xmax:
  for j in ymax:
    if [i,j] in hw:
      continue
    else:
      flag=False

if flag:
  ans=xnum[-1][0]+ynum[-1][0]-1
else:
  ans=xnum[-1][0]+ynum[-1][0]
print(ans)

### FIXED CODE ###

import bisect
h,w,m=map(int,input().split())
xnum=[[0,i] for i in range(h)]
ynum=[[0,i] for i in range(w)]
hw=[]
xmax=[]
ymax=[]
for i in range(m):
  s,t=map(int,input().split())
  xnum[s-1][0]+=1
  ynum[t-1][0]+=1
  hw.append([s-1,t-1])
hw.sort()
xnum.sort()
ynum.sort()
xmax.append(xnum[-1][1])
ymax.append(ynum[-1][1])
for i in range(2,len(xnum)+1):
  if xnum[-1*i][0]==xnum[-1][0]:
    xmax.append(xnum[-1*i][1])
  else:
    break
for i in range(2,len(ynum)+1):
  if ynum[-1*i][0]==ynum[-1][0]:
    ymax.append(ynum[-1*i][1])
  else:
    break
flag=True
for i in xmax:
  for j in ymax:
    l=bisect.bisect_left(hw,[i,j])
    r=bisect.bisect_right(hw,[i,j])
    if [i,j] in hw[l:r]:
      continue
    else:
      flag=False
      break

if flag:
  ans=xnum[-1][0]+ynum[-1][0]-1
else:
  ans=xnum[-1][0]+ynum[-1][0]
print(ans)
### BUGGY CODE ###

from collections import Counter
N = int(input())
list = []
for i in range(0,N):
    list.append(input())
counted = Counter(list)
res = [letter for letter,count in counted.most_common() if count == counted.most_common()[0][1]]
[print(i) for i in sorted(res)]

### FIXED CODE ###

from collections import Counter


N = int(input())
list = [input() for _ in range(0,N)]
counted = Counter(list)
res_max= counted.most_common()[0][1]
ans = []
for k, v in counted.items():
    if v == res_max:
        ans.append(k)

ans.sort()
print(*ans, sep="\n")
### BUGGY CODE ###

import sys
import math
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
mask=2^21-1
cnt=n
for i in range(n):
    j=i+1
    tmp=a[i]
    while(j<n):
        if tmp&a[j]==0:
            tmp+=a[j]
            cnt+=1
        else:
            break
        j+=1
print(cnt)

### FIXED CODE ###

import sys
import math
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
cnt=0
i=0
j=0
tmp=0
while(i<n):
    while(j<n):
        if tmp&a[j]==0:
            tmp+=a[j]
            cnt+=j-i+1
            # print(str(i)+" "+str(j))
        else:
            break
        j+=1
    tmp^=a[i]
    i+=1
print(cnt)
### BUGGY CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[float('inf')]*(N+K+1)
dp[0]=0
for i in range(N-1):
    for j in range(1,K+1):
        if i+j>N-1:
            break
        else:
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
print(dp[N-1])




### FIXED CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
dp=[float('inf')]*(N+1)
dp[0]=0
for i in range(N-1):
    for j in range(1,K+1):
        if i+j>N-1:
            break
        else:
            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i]-h[i+j]))
print(dp[N-1])
### BUGGY CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M, K = map(int, input().split())

uf = UnionFind(N)
L = [set() for _ in range(N)]

for _ in range(M):
  A, B = map(lambda x: int(x)-1, input().split())
  uf.union(A, B)
  L[A].add(B)
  L[B].add(A)

for _ in range(K):
  C, D = map(lambda x: int(x)-1, input().split())
  L[C].add(D)
  L[D].add(C)

ans = []

for i in range(N):
  ans.append(len(set(uf.members(i)) - L[i]) - 1)

print(*ans)

### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M, K = map(int, input().split())

uf = UnionFind(N)
Fr = [set() for _ in range(N)]

for _ in range(M):
  A, B = map(lambda x: int(x)-1, input().split())
  uf.union(A, B)
  Fr[A].add(B)
  Fr[B].add(A)

ans = []

for i in range(N):
  ans.append(uf.size(i) - len(Fr[i]) - 1)

for _ in range(K):
  C, D = map(lambda x: int(x)-1, input().split())
  if uf.same(C, D):
    ans[C] -= 1
    ans[D] -= 1

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

#!/usr/bin/env python3
import sys

YES = "Yes"
NO = "No"

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

def prod(A):
    ans = A[0]
    for i in range(1,len(A)):
        ans = ans * A[i]
    return ans

scores = [0] * K
pre = 0
i = 0
while i < K:
    scores[i] = A[i]
    i += 1

pre = prod(scores)

while i < N:
    cur = pre
    cur = cur / scores[i % K]
    scores[i % K] = A[i]
    cur = cur * A[i]

    if i >= K:
        if cur > pre:
            print(YES)
        else:
            print(NO)
    pre = cur
    i += 1

### FIXED CODE ###

#!/usr/bin/env python3
import sys

YES = "Yes"
NO = "No"

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

scores = [0] * K
scores[0] = A[0]
i = 1
while i < K:
    scores[i] = A[i]
    i += 1

while i < N:
    if A[i] > scores[i % K]:
        print(YES)
    else:
        print(NO)
    scores[i % K] = A[i]
    i += 1
### BUGGY CODE ###

import itertools

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


def mindist(XLIST):
    dist = float('Inf')
    for i in itertools.permutations(XLIST, len(XLIST)):
        dist_ = 0
        curx = 0
        for j in range(len(i)):
            dist_ += abs(i[j]-curx)
            curx = i[j]
        if (dist_ < dist):
            dist = dist_
    return(dist)

dist = float('Inf')
for i in range(N - K + 1):
    dist_ = mindist(X[i:(i+K)])
    if (dist_ < dist):
        dist = dist_

print(dist)


### FIXED CODE ###

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

dist = []
for i in range(N-K+1):
    dist.append(abs(X[i]) + abs(X[K+i-1]-X[i]))
    dist.append(abs(X[K+i-1]) + abs(X[i]-X[K+i-1]))

if not len(dist) == 0:
    print(min(dist))
else:
    print(0)
### BUGGY CODE ###

n,k,q = map(int,input().split())
a = [] # 順番
for _ in range(q):
    a.append(int(input()))


point = [k] * n


for win in a:         # 勝った人(a[win-1])以外を下げる
    point[win-1] += 1
    for i in range(n):
        point[i] -= 1
#        print(point)

#print(point)


for i in point:
    if i <= 0:
        print("No")
    elif i >= 1:
        print("Yes")



### FIXED CODE ###

n,k,q = map(int,input().split()) #N人、Kpoint、Q回答
a = [] # 順番
for _ in range(q):
    a.append(int(input()))

# 最初に回数分引く
point = [k-q] * n

for win in a:
    point[win-1] += 1

#print(point)


for i in point:
    if i <= 0:
        print("No")
    elif i >= 1:
        print("Yes")
### BUGGY CODE ###

from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
sum = 0
count = 0
a_sum = 0
res = []
b_list = []
for bt in b:
    sum += int(bt)
    if sum <= k:
        count += 1
        b_list.append(sum)
    else:
        break
#print(b_list)
res.append(count)
count = 0
ia = 1
for at in a:
    a_sum += int(at)
    if k < a_sum:
        print(max(res))
        sys.exit()
    count = ia
    sum = a_sum
    for bsum in b_list:
        sum = a_sum + bsum
        if k < sum:
            break
        else:
            count += 1
    ia += 1
    res.append(count)
print(max(res))


### FIXED CODE ###

from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = int(l[2])
n = int(l[0])
m = int(l[1])

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

b = [0]
for i in range(m):
    sum += B[i]
    b.append(sum)
#print(b)

ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
N=int(input())
S=list(map(int,input().split()))

ans=10**10

ave=0
for i in range(N):
    ave+=S[i]
ave=ave//N

for i in range(ave-N-1000,ave-N+1000):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei
        
print(ans)

### FIXED CODE ###

# coding: utf-8
# Your code here!
import statistics

N=int(input())
S=list(map(int,input().split()))

S1=[]
for i in range(N):
    S1.append(S[i]-i)
S1.sort()
chuouchi=statistics.median_high(S1)
chuouchi2=statistics.median_low(S1)
chuouchi3=statistics.median(S1)
ans=10**20
ansj=0
A=chuouchi-4
B=chuouchi+4
C=chuouchi2-4
D=chuouchi2+4
E=int(chuouchi3)-4
F=int(chuouchi3)+4
for i in range(A,B):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei
        
for i in range(C,D):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei

for i in range(E,F):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei        
        
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
from collections import defaultdict,deque
import bisect
import numpy as np

input = sys.stdin.readline
N = int(input())
a = list(map(int,input().split()))
g = defaultdict(list)
for i in range(N-1):
    u, v = map(int,input().split())
    g[u].append(v)
    g[v].append(u)

dq = deque([1])
visited = [False]*(N+1)

inf = 10**10
l = np.array([inf] * (N+1))

lis = [0] * (N+1)
lis[1] = 1

def f(v,l,k):
    if visited[v]:
        return
    visited[v] = True
    A = a[v-1]
    #i = bisect.bisect_left(l,A)
    i = np.searchsorted(l,A)
    l[i] = A 
    if k < i:
        k += 1
    lis[v] = k
    #lis[v] = l.index(inf)
    for nv in g[v]:
        if not visited[nv]:
            f(nv,np.copy(l),k)

l = [inf] * (N+1)
f(1,l,0)

for i in range(1,N+1):
    print(lis[i]+1)







### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)
from collections import defaultdict,deque
import bisect
input = sys.stdin.readline

N = int(input())
a = list(map(int,input().split()))

g = defaultdict(list)
for i in range(N-1):
    u, v = map(int,input().split())
    g[u].append(v)
    g[v].append(u)

def eulerTour(g,s):
    n = len(g)
    dq = [s]
    ls = [-1]*(n+1)
    rs = [-1]*(n+1)
    index = []
    visited = [False]*(n+1)
    def dfs(v):
        visited[v] = True
        ls[v] = len(index)
        index.append(v)
        for nv in g[v]:
            if not visited[nv]:
                dfs(nv)
        rs[v] = len(index)
        index.append(v)
    dfs(s)
    return ls,rs,index

ls,rs,index = eulerTour(g,1)
dq = deque([1])
visited = [False]*(N+1)

inf = 10**10
l = [inf] * (N+1)

ans = [0] * (N+1)
ans[1] = 1

visited = [False]*(N+1)
rollback = []

k = 1
for v in index:
    if visited[v]:
        i, pa = rollback.pop()
        l[i] = pa
        if pa == inf:
            k -= 1
    else:
        A = a[v-1]
        i = bisect.bisect_left(l,A)
        rollback.append((i,l[i]))
        if k <= i:
            k += 1
        l[i] = A
        #ans[v] = l.index(inf)
        ans[v] = k
        visited[v] = True


for i in range(1,N+1):
    print(ans[i])
### BUGGY CODE ###

N=int(input())
List = [input() for i in range(N)]

get=[]
      

for i in range(len(List)):
  if get.count(List[i])==0:
    get.append(List[i])

print(len(get))




### FIXED CODE ###

import collections

Row = int(input())
List = [input() for i in range(Row)]
 
c=collections.Counter(List)

  
print(len(list(c.keys())))
### BUGGY CODE ###

R, G, B, N = map(int, input().split())

rgb = []
for r in range(N+1):
    for g in range(N+1):
        if N-r*R-g*G >= 0 and (N-r*R-g*G)%B == 0:
            rgb.append((r,g,(N-r*R-g*G)//B))

print(len(set(rgb)))

### FIXED CODE ###

R, G, B, N = map(int, input().split())

ans = 0
for r in range(N//R + 1):
    for g in range((N - r*R)//G + 1):
        tmp = N-r*R-g*G
        if tmp%B == 0:
            ans += 1
print(ans)
### BUGGY CODE ###

m1 = 1046527
_dict = [""] * m1


def insert(k):
    i = 0

    while True:
        ix = _h(k, i)
        if _dict[ix] == "":
            _dict[ix] = k
            return
        else:
            i += 1


def find(k):
    i = 0

    while True:
        ix = _h(k, i)
        if _dict[ix] == k:
            return True
        elif _dict[ix] == "":
            return False
        else:
            i += 1

    return False


def _h(k, i):
    return ((k % m1) + i * (1 + k % (m1 - 1))) % m1




def getChar(ch):
    if ch == 'A':
        return 1
    elif ch == 'C':
        return 2
    elif ch == 'G':
        return 3
    elif ch == 'T':
        return 4
    else:
        return 0

def getKey(strings):
    Sum = 0
    p = 1
    for i in range(len(strings)):
        Sum += p * getChar(strings[i])
        p *= 5

    return Sum


def to_int(s):
    ret = 0
    i = 1
    for j in range(len(s)):
        e = s[len(s) - j - 1]

        if e == 'A':
            ret += 1 * i
        elif e == 'C':
            ret += 2 * i
        elif e == 'G':
            ret += 3 * i
        elif e == 'T':
            ret += 4 * i
        i *= 5

    return ret


if __name__ == '__main__':
    n = int(input())

    for _ in range(n):
        cmd, val = input().split()

        if cmd[0] == "i":
            insert(getKey(val))
        else:
            ans = "yes" if find(getKey(val)) else "no"
            print(ans)

### FIXED CODE ###

M = 1046527
L = 14
H = [""] * M
 
def getChar(ch):
    if ch == 'A':
        return 1
    elif ch == 'C':
        return 2
    elif ch == 'G':
        return 3
    elif ch == 'T':
        return 4
    else:
        return 0
 
def getKey(strings):
    Sum = 0
    p = 1
    for i in xrange(len(strings)):
        Sum += p * getChar(strings[i])
        p *= 5
 
    return Sum
 
def h1(key):
    return key % M
 
def h2(key):
    return 1 + (key % (M-1))
 
def find(strings):
    key = getKey(strings)
    i = 0
    while True:
        h = (h1(key) + i * h2(key)) % M
        if H[h] == strings:
            return 1
        elif len(H[h]) == 0:
            return 0
        else:
            i += 1
    return 0
 
def insert(strings):
    key = getKey(strings)
    i = 0
    while True:
        h = (h1(key) + i * h2(key)) % M
        if H[h] == strings:
            return 1
        elif len(H[h]) == 0:
            H[h] = strings
            return 0
        else:
            i += 1
    return 0
 
def main():
    n = input()
    for i in xrange(n):
        com = raw_input().split()
        if com[0][0] == 'i':
            insert(com[1])
        else:
            if find(com[1]):
                print 'yes'
            else:
                print 'no'
    return 0
 
main()
### BUGGY CODE ###

def f(x,i,n):
    sadness = 0
    for j in range(n):
        sadness += abs(x[j]-i)
    return(sadness)
N = int(input())
H = [int(i) for i in input().split()]
for i in range(N):
    H[i] -= i+1
H = sorted(H)
D = f(H,1,N)

for i in range(N):
    D = min(D,f(H,H[i],N))
print(D)


### FIXED CODE ###

def f(x,i,n):
    sadness = 0
    for j in range(n):
        sadness += abs(x[j]-i)
    return(sadness)
N = int(input())
H = [int(i) for i in input().split()]
for i in range(N):
    H[i] -= i+1
H = sorted(H)
k = N // 2
if N == 1 :
    print(0)
else:
    print(f(H,H[k],N))
### BUGGY CODE ###

N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if S[i] != S[j]:
            other = list({'R', 'G', 'B'} - {S[i], S[j]})[0]
            tmp = j + j - i
            for k in range(j+1, N):
                if (S[k] == other) and (tmp != k):
                    ans += 1
print(ans)





### FIXED CODE ###

N = int(input())  # 1 <= N <= 4000
S = input()  # [RGB]{N}
L = {
    'R': tuple(i for i, v in enumerate(S) if v == 'R'),
    'G': tuple(i for i, v in enumerate(S) if v == 'G'),
    'B': tuple(i for i, v in enumerate(S) if v == 'B')
}
R_length = len(L['R'])
G_length = len(L['G'])
B_length = len(L['B'])

ans = R_length * G_length * B_length
for i in range(N):
    for j in range(i+1, N):
        k = j + j - i
        if k >= N:
            break
        if S[i] != S[j] and S[i] != S[k] and S[j] != S[k]:
            ans -= 1
print(ans)
### BUGGY CODE ###

N = int( input() )
A = list( map( int, input().split() ) )
#dp = [ 10**4 for x in range(N) ]
#dp[i] i+1まで数える最短のmonotonically increasing列の位置

for i in range(N+1) :
    if i+1 in A : 
#       dp[i]=A.index(i+1)
       A = A[A.index(i+1)+1:]
    else :
       break
if i == 0:
   print( -1 )
else: 
   print( N - i )

### FIXED CODE ###

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

seekfor = 1
crack = 0

for i in range(N) :
    if A[i] == seekfor :
       seekfor += 1
    else : 
       crack += 1
    
if crack == N: 
    print( -1 )
else:    
    print(crack)