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

import sys
N=int(input())
L=list(map(int,input().split()))
if 1 not in L:
  print(-1)
  sys.exit()
for i in range(1,N+1):
  if i not in L:
    print(N+1-i)
    sys.exit()
  else:
    a=L.index(i)
    L=L[a:]
print(0)

### FIXED CODE ###

N=int(input())
L=list(map(int,input().split()))
i=0
n=1
while True:
  if i==N:
    break
  if L[i]==n:
    n+=1
  i+=1
if n==1:
  print(-1)
else:
  print(N-n+1)
### BUGGY CODE ###

def main():
  n=int(input())
  t=0
  for x in range(1,n+1):
    for y in range(1,x+1):
      if x*y==n:
        return x+y-2

if __name__ == '__main__':
  print(main())

### FIXED CODE ###

n=int(input());print(min(x+n//x-2for x in range(1,int(n**.5)+1)if n%x<1))
### BUGGY CODE ###

import numpy as np
ans=1000000000000
N=int(input())
for i in range(1,N+1):
    if (N%i==0):
        j=N//i
        move=(i-1)+(j-1)
        ans=min(ans,move)
print(ans)

### FIXED CODE ###

import numpy as np
ans=1000000000000
N=int(input())
for i in range(1,int(np.sqrt(N)+1)):
    if (N%i==0):
        j=N//i
        move=(i-1)+(j-1)
        ans=min(ans,move)
print(ans)
### BUGGY CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]
a_set = set(a)
result = 0
def ct(i, a, result):
    cnt = a.count(i)
    if cnt % 2 == 1:
        result += 1
    return result

for i in a_set:
    result = ct(i, a, result)

print(result)

### FIXED CODE ###

import collections
n = int(input())
a = [int(input()) for _ in range(n)]
result = 0
cnt = collections.Counter(a)
for i in cnt.values():
    if i % 2 == 1:
        result += 1

print(result)
### BUGGY CODE ###

n,t = map(int,input().split())
a = list(map(int,input().split()))
ans = []
flag = max(a)
flag_2 = a.index(flag)
for i in range(n):
  if i <= flag_2:
    ans.append(flag-a[i])
  else:
    flag = max(a[i:n])
    flag_2 = a.index(flag)
    ans.append(flag-a[i])
print(ans.count(max(ans)))

### FIXED CODE ###

n,t = map(int,input().split())
a = list(map(int,input().split()))
ans = [0]*n
flag = float("inf")
for i in range(n):
    flag = min(flag,a[i])
    ans[i] = a[i]-flag
print(ans.count(max(ans)))
### BUGGY CODE ###

def resolve():
    import numpy as np
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    hs = [[0]*w for _ in range(h)]
    ws = [[0]*w for _ in range(h)]

    for i in range(h):
        cums = 0
        for j in range(w):
            if s[i][j] == "#":
                cums = 0
                ws[i][j] = 0
            else:
                cums += 1
                ws[i][j] = cums
        for j in reversed(range(w-1)):
            if ws[i][j+1] != 0 and ws[i][j] != 0:
                ws[i][j] = ws[i][j+1]

    for i in range(w):
        cums = 0
        for j in range(h):
            if s[j][i] == "#":
                cums = 0
                hs[j][i] = 0
            else:
                cums += 1
                hs[j][i] = cums
        for j in reversed(range(h-1)):
            if hs[j+1][i] != 0 and hs[j][i] != 0:
                hs[j][i] = hs[j+1][i]
    print(np.max(np.array(ws) + np.array(hs))-1)


resolve()

### FIXED CODE ###

def resolve():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    hs = [[0]*w for _ in range(h)]
    ws = [[0]*w for _ in range(h)]

    for i in range(h):
        cums = 0
        for j in range(w):
            if s[i][j] == "#":
                cums = 0
                ws[i][j] = 0
            else:
                cums += 1
                ws[i][j] = cums
        for j in reversed(range(w-1)):
            if ws[i][j+1] != 0 and ws[i][j] != 0:
                ws[i][j] = ws[i][j+1]

    for i in range(w):
        cums = 0
        for j in range(h):
            if s[j][i] == "#":
                cums = 0
                hs[j][i] = 0
            else:
                cums += 1
                hs[j][i] = cums
        for j in reversed(range(h-1)):
            if hs[j+1][i] != 0 and hs[j][i] != 0:
                hs[j][i] = hs[j+1][i]

    ans = 0
    for i in range(h):
        for j in range(w):
            ans = max(ans, ws[i][j] + hs[i][j])
    print(ans-1)
resolve()
### BUGGY CODE ###

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

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

### 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 = int(input())
S = input()

s = 0
for a in range(N):
    o = S[a]
    for b in range(a+1, N):
        p = S[b]
        if o == p:
            continue
        for c in range(b+1, N):
            q = S[c]
            if o != q and p != q:
                if b-a != c-b:
                    s += 1
print(s)


### FIXED CODE ###

N = int(input())
S = input()

s_l = {}
for i in range(N-1):
    t = {}
    s = S[i+1:]
    t['s'] = s
    t['R'] = s.count('R')
    t['G'] = s.count('G')
    t['B'] = s.count('B')
    s_l[i] = t

s = 0
for a in range(N-2):
    s1 = S[a]
    for b in range(a+1, N-1):
        s2 = S[b]
        if s1 == s2:
            continue
        n = s_l[b]['s']
        pat = ['R', 'G', 'B']
        pat.remove(s1)
        pat.remove(s2)
        count = s_l[b][pat[0]]
        d = b - a
        if len(n) > d-1 and n[d-1] == pat[0]:
            count -= 1
        s += count

print(s)
### BUGGY CODE ###

import math
A, B = map(int, input().split())

cf=[]
for i in range(1,min(A,B)+1):
  if A % i == 0 and B % i == 0:
    cf.append(i)

def is_prime(n):
  if n == 1: return False

  for k in range(2, int(math.sqrt(n)) + 1):
    if n % k == 0:
      return False

  return True
    
cnt = 0
for i in range(len(cf)):
  if is_prime(cf[i]) == True:
    cnt += 1

print(cnt+1)

### FIXED CODE ###

from fractions import gcd

def prime_factorize(n):
  factor = []
  for i in range(2, int(n**0.5)+1):
    while n % i == 0:
      n //= i
      factor.append(i)
  if n != 1:
    factor.append(n)
  return factor

A, B = map(int, input().split())
N = gcd(A, B)
l = set(prime_factorize(N))
print(len(l)+1)
### BUGGY CODE ###

from bisect import bisect_left as BL 
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()

#border以上の幸福度を返す握手の組の数が、M以上であるか
def isok(border):
  res=0
  for i in range(N):
    res+=N-BL(A,border-A[i])
  return res>=M
    

ok=0
ng=A[-1]*2+1
mid=(ok+ng)//2

#幸福度h以上の握手がM組以上存在するような最大のhを探す
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if isok(mid):
    ok=mid
  else:
    ng=mid
  
ans=0
shake_c=0
for i in range(N):
  goodshake=N-BL(A,ok-A[i])
  shake_c+=goodshake
  for j in range(goodshake):
    ans+=A[i]+A[-1-j]
    
ans-=(shake_c-M)*ok
print(ans)
  

### FIXED CODE ###

from bisect import bisect_left as BL 
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()

#border以上の幸福度を返す握手の組の数が、M以上であるか
def isok(border):
  res=0
  for i in range(N):
    res+=N-BL(A,border-A[i])
  return res>=M
    

ok=0
ng=A[-1]*2+1
mid=(ok+ng)//2

#幸福度h以上の握手がM組以上存在するような最大のhを探す
while abs(ok-ng)>1:
  mid=(ok+ng)//2
  if isok(mid):
    ok=mid
  else:
    ng=mid
  
B=reversed(A)
SB=[0]
for b in B:
  SB.append(b+SB[-1])
  
ans=0
shake_c=0
for i in range(N):
  goodshake=N-BL(A,ok-A[i])
  shake_c+=goodshake
  ans+=A[i]*goodshake
  ans+=SB[goodshake]  
ans-=(shake_c-M)*ok
print(ans)
### BUGGY CODE ###

import itertools

N = int(input())

data = []
min_value_0 = 0
for _ in range(N):
	X, Y, P = town = tuple(map(int, input().split()))
	data.append(town)
	min_value_0 += min(abs(X), abs(Y)) * P

print(min_value_0)

B = 2 ** N
table_h = []
table_v = []

for b in range(B):
	towns = []
	i = 0
	x = b
	while x > 0:
		if x % 2 == 1:
			towns.append(i)
		x //= 2
		i += 1
	dists_h = []
	dists_v = []
	for i in range(N):
		X0, Y0, P0 = data[i]
		d_h = abs(X0)
		d_v = abs(Y0)
		for j in towns:
			X1, Y1, P1 = data[j]
			d_h = min(d_h, abs(X1 - X0))
			d_v = min(d_v, abs(Y1 - Y0))
		dists_h.append(d_h * P0)
		dists_v.append(d_v * P0)
	table_h.append(dists_h)
	table_v.append(dists_v)
#	print(f"{{:0{N}b}}".format(b), dist_h, dist_v)

table = [min_value_0] * (N + 1)

import sys

for t in range(3 ** N):
	b0 = 0
	b1 = 0
	b = 1
	c = 0
	while t > 0:
		r = t % 3
		if r == 1:
			b0 += b
			c += 1
		elif r == 2:
			b1 += b
			c += 1
		b *= 2
		t //= 3
	value = 0
	t1 = table_h[b0]
	t2 = table_v[b1]
	for i in range(N):
		value += min(t1[i], t2[i])
	if table[c] > value:
		table[c] = value

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

print(0)


### FIXED CODE ###

import itertools

N = int(input())

data = []
min_value_0 = 0
for _ in range(N):
	X, Y, P = town = tuple(map(int, input().split()))
	data.append(town)
	min_value_0 += min(abs(X), abs(Y)) * P

print(min_value_0)

B = 2 ** N
table_h = []
table_v = []

for b in range(B):
	towns = []
	i = 0
	x = b
	while x > 0:
		if x % 2 == 1:
			towns.append(i)
		x //= 2
		i += 1
	dists_h = []
	dists_v = []
	for i in range(N):
		X0, Y0, P0 = data[i]
		d_h = abs(X0)
		d_v = abs(Y0)
		for j in towns:
			X1, Y1, P1 = data[j]
			d_h = min(d_h, abs(X1 - X0))
			d_v = min(d_v, abs(Y1 - Y0))
		dists_h.append(d_h * P0)
		dists_v.append(d_v * P0)
	table_h.append(dists_h)
	table_v.append(dists_v)
#	print(f"{{:0{N}b}}".format(b), dist_h, dist_v)

table = [min_value_0] * (N + 1)

import sys

stack = [0] * N
i = 0
c = 0
b0 = 0
b1 = 0
b = 1
while True:
	if i == N:
		value = 0
		t1 = table_h[b0]
		t2 = table_v[b1]
		for j in range(N):
			value += min(t1[j], t2[j])
		if table[c] > value:
			table[c] = value
		i -= 1
		b //= 2
	else:
		s = stack[i]
		if s == 3:
			if i == 0:
				break
			else:
				stack[i] = 0
				i -= 1
				b //= 2
		else:
			if s == 0:
				b0 += b
				c += 1
			elif s == 1:
				b0 -= b
				b1 += b
			else:
				b1 -= b
				c -= 1
			stack[i] = s + 1
			i += 1
			b *= 2

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

print(0)
### BUGGY CODE ###

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    X, Y, Z, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()] 
    B = [int(i) for i in input().split()]
    C = [int(i) for i in input().split()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  # A.sort(reverse=True)
  # B.sort(reverse=True)
  # C.sort(reverse=True)

  AB = []
  for i in A:
    for j in B:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  if 'get_ipython' in globals(): 
    print(AB)
  
  ABC = []
  for i in AB[0:K]:
    for j in C:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)
  
  if 'get_ipython' in globals(): 
    print(ABC)

  for i in ABC[0:K]:
    print(i)      

main()

### FIXED CODE ###

def main():
  if 'get_ipython' not in globals():  
    # For Subsession
    X, Y, Z, K = (int(i) for i in input().split())
    A = [int(i) for i in input().split()] 
    B = [int(i) for i in input().split()]
    C = [int(i) for i in input().split()] 
  else:
    A = [1,10,100]
    B = [2,20,200]
    C = [1,10,100]
    #A = [4,6]
    #B = [1,5]
    #C = [3,8]
    X = len(A)
    Y = len(B)
    Z = len(C)
    K = 5
    #K = 8

  # Processing
  AB = []
  A.sort(reverse=True)
  B.sort(reverse=True)
  for i in A[0:K]:
    for j in B[0:K]:
      AB.append(i+j)
  
  AB.sort(reverse=True)
  
  ABC = []
  C.sort(reverse=True)
  
  for i in AB[0:K]:
    for j in C[0:K]:
      ABC.append(i+j)  
  
  ABC.sort(reverse=True)

  for i in ABC[0:K]:
    print(i)      

main()
### BUGGY CODE ###

import collections
n=int(input())
A = sorted(list(map(int,input().split())))
for i in range(1,n+1):
    print(A.count(i))
    del A[:A.count(i)]
    

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n-1):
  b[a[i]-1]+=1
for i in b:
  print(i)
### BUGGY CODE ###

S = input()
ans =0
for i in range(1,10**5):
  if S.count('0') ==0 or S.count('1') ==0:
      break
  for j in range(1,10**5):
      if S[j] != S[j-1]:
          S= S[0:j-1]+S[j+1:]
          ans +=2
          break
    
print(ans)

### FIXED CODE ###

S=input()

ans = min(S.count('0'), S.count('1'))

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

import math
import sys

n, m = map(int, input().split())
a_list = [-1]
for _ in range(m):
    a_list.append(int(input()))
a_list.append(n + 1)

memo = dict()
ans = 1
cur = a_list[0]
for i, a in enumerate(a_list):
    if i == 0:
        continue
    sub = a - cur - 1
    cur = a
    if sub <= 2:
        if sub == 0:
            print(0)
            sys.exit()
        continue
    sub -= 1
    if sub in memo.keys():
        ans *= memo[sub]
        continue
    count = 0
    for j in range(sub // 2 + 1):
        count += math.factorial(sub - j) // math.factorial(j) // math.factorial(sub - j * 2)
    memo[sub] = count
    ans *= count
print(ans % 1000000007)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline

N, M = map(int, readline().split())
ans = [0] * (N + 2)
ans[0] = 1
for _ in range(M):
    a = int(readline())
    ans[a] = -1

for i in range(1, N + 1):
    if ans[i] == -1:
        continue
    a1 = max(ans[i - 2], 0)
    a2 = max(ans[i - 1], 0)
    ans[i] = a1 + a2

print(ans[N] % (10 ** 9 + 7))
### BUGGY CODE ###

N=int(input())
s=[int(input())  for i in range(N)]
ans=0
for k in range(2**N):
    t=0
    for j in range(N):
        if k&2**j:
            t+=s[j]
    if t%10==0:
        continue
    ans=max(ans,t)
    ans=t
print(ans)  

### FIXED CODE ###

N=int(input())
s=sorted([int(input())  for i in range(N)])
if sum(s)%10!=0:
    print(sum(s))
else:
    for j in range(N):
        if s[j]%10!=0:
            print(sum(s)-s[j])
            break
    else:
        print(0)
### BUGGY CODE ###

N=int(input())
s=[list(input()) for _ in range(N)]

count=0

ss=[]
tt=[]

for i in range(N-1):
    for j in range(N-i-1):
        ss=sorted(s[i])
        tt=sorted(s[i+j+1])
        if ss==tt:
            count+=1

print(count)

### FIXED CODE ###

N=int(input())
s=[input() for _ in range(N)]

S=[""]*N
T={}
count=0

for i in range(N):
    S[i]=sorted(s[i])
    S[i]="".join(S[i])
    if S[i] not in T:
        T[S[i]]=1
    else:
        count+=T[S[i]]
        T[S[i]]+=1

print(count)
### BUGGY CODE ###

from sys import stdin
import numpy as np
from copy import deepcopy
import math
import fractions
from functools import reduce

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)

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

solve = []
for i in range(len(A)):
  lst = deepcopy(A)
  lst.pop(i)
  var = gcd_list(lst)
  solve.append(var)

ans = max(solve)
print(ans)
  
  

### FIXED CODE ###

# あまりにもTLEになるから解説見ちゃった

from sys import stdin
import numpy as np
from copy import deepcopy
from fractions import gcd

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

"""
## 入力生成用
import random
N = 1000
A = [random.randint(1,1e9) for i in range(N)]
"""

solve = []
L = np.zeros(N)
R = np.zeros(N)

for i in range(N-1):
  L[i+1] = gcd(L[i], A[i])
  R[N-2-i] = gcd(R[N-1-i], A[N-1-i])
  
for i in range(N):
  var = gcd(L[i], R[i])
  solve.append(var)

ans = int(max(solve))
print(ans)
### BUGGY CODE ###

n,m = input().split(" ")
n = int(n)
m = int(m)

arr = [[] for _ in range(m)]

for i in range(n):
  a,b = input().split(" ")
  a = int(a)
  b = int(b)
  
  if a > m:
    continue
    
  arr[a-1].append(b)

vs = []
res = 0
for item in arr:
  vs.extend(item)
  if len(vs) == 0:
    continue
    
  vs.sort()
  maxi = vs.pop(-1)
  res += maxi
  
print(res)

### FIXED CODE ###

import heapq
n,m = input().split(" ")
n = int(n)
m = int(m)

arr = [[] for _ in range(m)]

for i in range(n):
  a,b = input().split(" ")
  a = int(a)
  b = - int(b) # for heap
  
  if a > m:
    continue
    
  arr[a-1].append(b)

vs = []
heapq.heapify(vs)

res = 0
for item in arr:
  for v in item:
	  heapq.heappush(vs, v)
  
  if len(vs) == 0:
    continue
    
  maxi = heapq.heappop(vs)
  res += (-1* maxi)
  
print(res)
### BUGGY CODE ###

N = int(input())
S = int(input().replace('W', '1').replace('E', '0'), 2)
print(min([bin(S ^ 2 ** i - 1).count('1') for i in range(N + 1)]))

### FIXED CODE ###

N = int(input())
S = input()

dp = S[1 : N].count('E')
ans = dp
for i in range(N - 1):
    if S[i] == 'W':
        dp += 1
    if S[i + 1] == 'E':
        dp -= 1
    ans = min(dp, ans)
print(ans)
### BUGGY CODE ###

N = int(input())
S = input()
ans = 0
ans = 0
l, r = 0, 1

while l<N:
    s1 = S[l]
    v = 0
    while r<N:
        s2 = S[r]
        if s1!=s2:
            r+=1
            continue
        v = 1
        for i in range(1, r-l):
            if r+i>=N or l+i>=r:break
            v+=1
            if S[l+i]!=S[r+i]:continue
        ans = max(ans, v)
        r+=v
    l+=1
    r=l+1
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
dp = [[0]*(N+1) for _ in range(N+1)]

for l in range(N)[::-1]:
    for r in range(l+1, N)[::-1]:
        dp[l][r] = dp[l+1][r+1]+1 if S[l]==S[r] else 0

ans = 0 
for l in range(N)[::-1]:
    for r in range(l+1, N)[::-1]:
        ans = max(ans, min(r-l, dp[l][r]))
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
number = [0] * N
total = 0
for i in range(N):
  tmp = A.count(i + 1)
  total += tmp * (tmp -1) // 2
  number[i] = tmp
for i in range(N):
  number[i] = total - number[i] + 1
for j in A:
  print(number[j-1])


### FIXED CODE ###

import numpy as np
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))
number = np.zeros(N, dtype = int)
total = 0

dic = {}
a = sorted(A)
now_key = a[0]
value = 1
dic[now_key] = 1
for i in range(1,N):
  if a[i] == now_key:
    value += 1
  else:
    dic[now_key] = value
    if value >= 2:
      total += value * (value-1) // 2
    now_key = a[i]
    value = 1
dic[now_key] = value
if value >= 2:
  total += value * (value-1)//2
for j in A:
  print(total - dic[j] + 1)
### BUGGY CODE ###

N,M = map(int,input().split())
K=[]
A=[]
B=[]
answer='IMPOSSIBLE'
for i in range(M):
  a,b=map(int,input().split())
  A +=[a]
  B += [b]
  if a==1 and b==N:
    answer='POSSIBLE'
  if a==1:
    K +=[b]
for i in range(M):
  if A[i] in K and B[i]==N:
    answer='POSSIBLE'
print(answer)

### FIXED CODE ###

N,M = map(int,input().split())
K=[0]*(N+1)
for i in range(M):
  a,b=map(int,input().split())
  if a==1:
    K[b] +=1
  if b == N:
    K[a] +=1
print('POSSIBLE' if 2 in K else 'IMPOSSIBLE')
### BUGGY CODE ###


N = int(input())
s = []
for i in range(N):
    s.append(sorted(input()))

count = 0
for i in range(N):
    for j in range(i+1, N):
        if s[i] == s[j]:
            count += 1

print(count)

### FIXED CODE ###

import collections
import math

N = int(input())

al = "abcdefghijklmnopqrstuvwxyz"
al_dict = {}
for i, c in enumerate(al):
    al_dict[c] = i

ss = []
for i in range(N):
    counts = [0 for i in range(26)]
    for char in input():
        ind = al_dict[char]
        counts[ind] += 1
    counts = [str(count) for count in counts]
    key = ",".join(counts)
    #c = collections.Counter(s0)
    #print(tuple(c))
    ss.append(key)

#print(ss)
cc = collections.Counter(ss)

#print(cc)
out = 0
for count in cc.values():
    if count == 1:
        continue
    out += math.factorial(count) // (math.factorial(count-2) * 2)
print(out)

#for s in ss:
#    for  in s[i+1]:
#        if s[i] == s[j]:
#            count += 1

#print(count)
### BUGGY CODE ###

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

Dp = [None]*N
Dp[0] = 0

for i in range(1,N):
  Dp[i] =min([Dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-K),i)])

print(Dp[-1])


### FIXED CODE ###

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

Dp = [None]*N
Dp[0] = 0

for i in range(1,N):
  Dp[i] =min([Dp[j]+abs(h[i]-h[j]) for j in range(max(0,i-K),i)])

print(Dp[-1])
### BUGGY CODE ###

s = input()
q = int(input())
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		s = s[::-1]
	elif tfc[1] == '1':
			s = tfc[2] + s
	else:
			s += tfc[2]
print(s)

### FIXED CODE ###

from collections import deque

s = input()
q = int(input())
revflag = 1
d = deque(s)
for i in range(q):
	tfc = list(map(str, input().split()))
	if tfc[0] == '1':
		revflag *= -1
	elif tfc[1] == '1':
		if revflag > 0:
			d.appendleft(tfc[2])
		else:
			d.append(tfc[2])
	else:
		if revflag > 0:
			d.append(tfc[2])
		else:
			d.appendleft(tfc[2])
if revflag > 0:
	print(''.join(d))
else:
	print(''.join(reversed(d)))
### BUGGY CODE ###

N,M=map(int,input().split())
A=list(map(int,input().split()))
dp=[2,5,5,4,5,6,3,7,6]
p=[]
for i in range(M):
  p.append([dp[A[i]-1],A[i]])
bp=[]
for i in range(N+1):
  bp.append('0')
for i in range(N-1):
  for j in range(len(p)):
    if i+p[j][0]>N:
      continue
    if bp[i]=='0':
        if i!=0:
            continue
    bp[i+p[j][0]]=str(max(int(bp[i+p[j][0]]),int(str(p[j][1])+bp[i])))
ans=bp[N]
ans=ans.rstrip('0')
print(ans)

### FIXED CODE ###

def main():  
  N,M=map(int,input().split())
  A=list(map(int,input().split()))
  dp=[2,5,5,4,5,6,3,7,6]
  p=[]
  q=[]
  for i in range(M):
    p.append(dp[A[i]-1])
    q.append(A[i])
  bp=[[0,0]for i in range(N+1)]
  for i in range(N-1):
    for j in range(len(p)):
      if i+p[j]>N:
        continue
      if bp[i][0]==0:
          if i!=0:
              continue
      bp[i+p[j]][0]=max(bp[i+p[j]][0],bp[i][0]+(10**bp[i][1])*q[j])
      bp[i+p[j]][1]=bp[i][1]+1
  print(bp[N][0])
if __name__=='__main__':
  main()
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split(' ')))
for i in range(n):
    rd = a.copy()
    del rd[i]
    co = 0
    for j in range(n - 2):
        co += rd[j + 1:].count(rd[j])
    print(co)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split(' ')))
c = [0] * (n + 1)
d = [0] * (n + 1)
for i in a:
    c[i] += 1
    d[i] = c[i] * (c[i] - 1) // 2
t = sum(d)
for i in a:
    if c[i] > 2:
        r = (((c[i] - 1) * (c[i] - 2)) // 2)
    else:
        r = 0
    print(t - d[i] + r)
### BUGGY CODE ###

N=int(input())
s=input()
S=list(s)


t=0
for i in range(N-1):
    if S[i-t+1] == S[i-t]:
        S.remove(S[i-t+1])
        t+=1
print(N-t)


### FIXED CODE ###

N=int(input())
S=list(input())

t=0
for i in range(N-1):
    if S[i+1] == S[i]:
        t+=1
print(N-t)
### BUGGY CODE ###

N=int(input())
data=list(map(int,input().split()))
s=1
for i in range(N):
    s *= data[i]
if s > 10**18:
    print(-1)
else:
    print(s)

### FIXED CODE ###

N=int(input())
data=list(map(int,input().split()))
if 0 in data:
    print(0)
else:
    s=1
    for f in data:
        s *= f
        if s > 10**18:
            break
    if s > 10**18:
        print(-1)
    else:
        print(s)
### BUGGY CODE ###

# ABC0555 - B - Training Camp

N=int(input())

ans=1

for i in range(1,N+1):
    ans*=i

if ans>=10**9+7:
    ans=ans%(10**9+7)

print(ans)


### FIXED CODE ###

# ABC0555 - B - Training Camp

N=int(input())

ans=1

for i in range(1,N+1):
    ans*=i
    ans%=(10**9+7)

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

import numpy as np
 
N = int(input())
d = np.array(list(map(int, input().split())))

ans = 0
for K in range(1, 10**5+1):
  if np.sum(d>=K) == np.sum(d<K): ans += 1
print(ans)

### FIXED CODE ###

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

d = sorted(d)
print(d[N//2] - d[(N//2)-1])