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

#https://atcoder.jp/contests/abc138/submissions/8926003

S = input()
T = input()
if not set(T) <= set(S):
    print(-1)
    exit()
now = -1
rep = 0
for t in T:
    now = S.find(t,now+1)
    if now == -1:
        rep += 1
        now = S.find(t)
print(rep*len(S)+now+1)


### FIXED CODE ###

from bisect import bisect_left
s = list(input())
t = list(input())
N = len(s)
if not set(t) <= set(s):
    print(-1)
    exit()
lis = [[] for _ in range(26)]
s = s+s
for i in range(2*N):
    alp = ord(s[i])-97
    lis[alp].append(i+1)
nex = [[0]*N for _ in range(26)]
for now in range(N):
    for alp in range(26):
        if not lis[alp]:
            continue
        i = bisect_left(lis[alp],now+1)
        #print(lis[alp],now+1,i)
        nex[alp][now] = lis[alp][i] - now
ans = 0
now = 0
tN = len(t)
for i in range(tN):
    alp = ord(t[i])-97
    ne = nex[alp][now]
    ans += ne
    if now+ne >= N:
        now += ne-N
    else:
        now += ne
    #print(now,ans)
print(ans)
### BUGGY CODE ###

X = input()
lenX = len(X)
si = 0
ei = 0
ans = lenX
for _ in range(lenX//2):
  try:
    si = X[si:].index('S') + si
    ei = X[max(si,ei):].index('T') + max(si,ei)
    if si<ei:ans-=2
    si += 1
    ei += 1
  except:
    break
print(ans)

### FIXED CODE ###

def sol():
  ti = 0
  ans = lenX
  for i in range(lenX//2):
    while S[i]>=T[ti]:
      ti += 1
      if ti>=lenX//2:return ans
    ans -= 2    
    ti += 1
    if ti>=lenX//2:return ans
  return ans

X = input()
lenX = len(X)
S = []
T = []
for i,x in enumerate(X):
  if x == 'S':S.append(i)
  else:T.append(i)

print(sol())
### BUGGY CODE ###

n=int(input())
numeros=[int (x) for x in input().split()]
cont=0
cont1=1
qtd=[]
K=1
while True:
    for num in numeros:
        if num<K:
            cont+=1
        else:
            cont1+=1
    if cont==cont1:
        qtd.append(K)
    K+=1
    if K>max(numeros):
        break
    cont=0
    cont1=0
print(len(qtd))

### FIXED CODE ###

n=int(input())
v=[int(i) for i in input().split()]
v=sorted(v)
j=int((n/2))
k=int((n/2)-1)
if v[j]!=v[k]:
    print(v[j]-v[k])
else:
    print("0")
### BUGGY CODE ###

def solve():
    input()
    s = input()
    ans = 0
    for i in range(1000):
        t = str(i)
        while(len(t)<3):
            t = '0'+t
        it = 0
        for i in s:
            if it==3:
                continue
            if i==t[it]:
                it+=1
        if it==3:
            ans+=1
    print(ans)
if __name__ == "__main__":
    solve()

### FIXED CODE ###

def solve():
    input()
    s = input()
    ans = 0
    for i in range(1000):
        t = str(i)
        while(len(t)<3):
            t = '0'+t
        it = 0
        for i in s:
            if it==3:
                break
            if i==t[it]:
                it+=1
        if it==3:
            ans+=1
    print(ans)
if __name__ == "__main__":
    solve()
### BUGGY CODE ###

n, q = map(int, input().split())
s = list(str(input()))

for i in range(q):
  ans = 0
  l, r = map(int, input().split())
  c = s[l-1: r]
  for j in range(r-l):
    if c[j] == "A" and c[j+1] == "C":
      ans +=1
  print(ans)    

### FIXED CODE ###

n, q = map(int, input().split())
s = list(str(input()))
dp = [0] * n
for j in range(n-1):
  if s[j] == "A" and s[j+1] == "C":
    dp[j+1] = 1 + dp[j]
  else:
    dp[j + 1] = dp[j]
for i in range(q):
  l, r = map(int, input().split())
  print(dp[r-1] - dp[l-1])
### BUGGY CODE ###

k = int(input())

sum = 0

def gcd(x, y): # ユークリッドの互除法
    if y > x:
        y,x = x,y
    while y > 0:
        r = x%y
        x = y
        y = r
    return x

import math

for i in range(1,k+1):
    for m in range(1,k+1):
        for l in range(1,k+1):
            sum += math.gcd(math.gcd(i,m),l)
print(sum)


### FIXED CODE ###

k = int(input())

sum = 0

def gcd(x, y): # ユークリッドの互除法
    if y > x:
        y,x = x,y
    while y > 0:
        r = x%y
        x = y
        y = r
    return x

import math

for i in range(1,k+1):
    for m in range(1,k+1):
        sub = gcd(i,m)
        for l in range(1,k+1):
            sum += gcd(sub,l)
print(sum)
### BUGGY CODE ###

N = int(input())
List = list(map(int, input().split()))
for i in range (N):
  for j in range (i+1, N):
    if List[j] == List[i]:
      print("NO")
      break
  else:
    continue
  break
else:
  print("YES")

### FIXED CODE ###

N = int(input())
List = sorted(list(map(int, input().split())))
for i in range(N-1):
  if List[i] == List[i + 1]:
    print("NO")
    break
else:
  print("YES")
### BUGGY CODE ###

N,M = map(int,input().split())
A = M//N
flag = False
for i in range(1,A+1):
  if (M%(A+1-i) == 0):
    flag = True
    break
if flag:
  print(A+1-i)

### FIXED CODE ###

import math

N,M = map(int,input().split())
A = M//N
L = []
for i in range(1,int(math.sqrt(M))+1):
  if (M%i == 0 and i <= A):
    L.append(i)
    if (M//i <= A):
      L.append(M//i)
print(max(L))
### BUGGY CODE ###

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

count = 0
while(True):
    val = n - a

    if val > 0:
        n = val
        count += a
    else:
        count = count + a  +  val
        break
    n -= b
    if n <= 0:
        break
print(count)


### FIXED CODE ###

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

ans = n // (a + b) * a
alp = min([n % (a + b), a])

print(ans + alp)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
a.sort()
a.append(0)
q=int(input())
for i in range(q):
    bi,ci=map(int,input().split())
    t=0
    for j in range(n):
        if t==1:
            break
        else:
            d=j
            while a[d]==bi:
                a[d]=ci
                d+=1
                t=1
    print(sum(a))

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
b=[0]
b*=10**5+1
for i in a:
    b[i]+=1
ans=sum(a)
q=int(input())
for i in range(q):
    bi,ci=map(int,input().split())
    d=b[bi]
    b[ci]+=d
    b[bi]=0
    ans=ans+d*(ci-bi)
    print(ans)
### BUGGY CODE ###

from heapq import heappush, heappop

n, q = map(int, input().split())
STX = [list(map(int, input().split())) for _ in range(n)]
task = []
for s, t, x in STX:
    task.append((t - x, 0, x))
    task.append((s - x, 1, x))
for i in range(q):
    d = int(input())
    task.append((d, 2, i))

task.sort()
kouho = set()
kouho_hp = []
ans = [-1] * q
for a, b, c in task:
    if b == 0:
        kouho.remove(c)
    if b == 1:
        kouho.add(c)
        heappush(kouho_hp, c)
    if b == 2:
        while kouho_hp and kouho_hp[0] not in kouho:
            heappop(kouho_hp)
        if not kouho_hp:
            continue
        else:
            ans[c] = kouho_hp[0]
        # heapは0-indexが最小値になっているからheapified_list[0]でもよい
print(*ans, sep="\n")


### FIXED CODE ###

from heapq import heappush, heappop
import sys

input = sys.stdin.buffer.readline


n, q = map(int, input().split())
STX = [list(map(int, input().split())) for _ in range(n)]
task = []
for s, t, x in STX:
    task.append((t - x, 0, x))
    task.append((s - x, 1, x))
for i in range(q):
    d = int(input())
    task.append((d, 2, i))

task.sort()
kouho = set()
kouho_hp = []
ans = [-1] * q
for a, b, c in task:
    if b == 0:
        kouho.remove(c)
    if b == 1:
        kouho.add(c)
        heappush(kouho_hp, c)
    if b == 2:
        while kouho_hp and kouho_hp[0] not in kouho:
            heappop(kouho_hp)
        if not kouho_hp:
            continue
        else:
            ans[c] = kouho_hp[0]
        # heapは0-indexが最小値になっているからheapified_list[0]でもよい
print(*ans, sep="\n")
### BUGGY CODE ###

import sys
n = int(sys.stdin.readline().strip())

eroot_n = int(pow(n, 0.5))
res = 0
for num_a in range(1, eroot_n + 1):
    for num_b in range(num_a, n):
        """
        print(num_a)
        print(num_b)
        print('\n')
        """
        if num_a * num_b >= n:
            continue
        if num_a == num_b:
            res = res + 1
        else:
            res = res + 2
print(res)

### FIXED CODE ###

import sys
n = int(sys.stdin.readline().strip())

eroot_n = int(pow(n, 0.5))
res = 0
for num_a in range(1, eroot_n + 1):
    limit_b = int(n / num_a) + 1
    for num_b in range(num_a, limit_b):
        """
        print(num_a)
        print(num_b)
        print('\n')
        """
        if num_a * num_b >= n:
            continue
        if num_a == num_b:
            res = res + 1
        else:
            res = res + 2
print(res)
### BUGGY CODE ###

n=int(input())

x=1
for i in range(n):
    x*=(i+1)

print(x%(10**9+7))


### FIXED CODE ###

n=int(input())
x=1
for i in range(n):
    x*=(i+1)
    if x>10**9+7:
        x=x%(10**9+7)

print(x)
#import numpy as np
#n=int(input())
#print(np.prod([i+1 for i in range(n)]))
#print(np.prod([i+1 for i in range(n)])%((10**9)+7))

#オーバーフロー？
### BUGGY CODE ###



N, Q = list(map(int, input().split()))
eda = [[] for i in range(N)]
ope = [0]*N
score = [0]*N
parent = [-1]*N


for i in range(N - 1):
	a, b = list(map(int, input().split()))
	eda[a - 1].append(b - 1)
	eda[b - 1].append(a - 1)

for i in range(Q):
	p, x = list(map(int, input().split()))
	ope[p - 1] += x


que = []
que.append(0)
score[0] = ope[0]
for ss in range(N - 1):
	loc = que.pop()
	for edge in eda[loc]:
		if parent[loc] != edge:
			score[edge] += score[loc] + ope[edge]
			que.append(edge)
			parent[edge] = loc

for i in range(N - 1):
	print(score[i], end = "")
	print(" ", end = "")
print(score[N - 1])

### FIXED CODE ###


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


N, Q = list(map(int, input().split()))
eda = [[] for i in range(N)]
ope = [0]*N
score = [0]*N
parent = [-1]*N


for i in range(N - 1):
	a, b = list(map(int, input().split()))
	eda[a - 1].append(b - 1)
	eda[b - 1].append(a - 1)

for i in range(Q):
	p, x = list(map(int, input().split()))
	ope[p - 1] += x


que = []
que.append(0)
score[0] = ope[0]
for ss in range(N - 1):
	loc = que.pop()
	for edge in eda[loc]:
		if parent[loc] != edge:
			score[edge] += score[loc] + ope[edge]
			que.append(edge)
			parent[edge] = loc

for i in range(N - 1):
	print(score[i], end = "")
	print(" ", end = "")
print(score[N - 1])
### BUGGY CODE ###

from sys import stdin
from collections import defaultdict 
# from bisect import *
# from heapq import *
# import math
# mod = 10**9+7

def primes(n):
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1, int(n**0.5)//3+1):
        if sieve[i]:
            k = 3*i+1|1
            sieve[k*k//3::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2, 3] + [3*i+1|1 for i in range(1, n//3-correction) if sieve[i]]

N = input()
A = list(map(int, input().split()))
cnt = [0] * (10**6+1)
ps = primes(10**6+1)
for p in ps:
    for a in A:
        if a % p == 0:
            cnt[p] += 1
if all([x < 2 for x in cnt]):
    print("pairwise coprime")
    exit()
gcd = lambda x, y : x if y == 0 else gcd(y, x % y)
x = A[0]
for a in A[1:]:
    x = gcd(x, a)
if x == 1:
    print("setwise coprime")
else:
    print("not coprime")

### FIXED CODE ###

from sys import stdin
from collections import defaultdict 
# from bisect import *
# from heapq import *
# import math
# mod = 10**9+7

N = input()
A = list(map(int, input().split()))
M = 10**6+1
spf = [i for i in range(M)]
for i in range(2, M):
    if spf[i] == i:
        for j in range(i+i, M, i):
            spf[j] = i
cnt = defaultdict(int)
for a in A:
    ps = set()
    while a != 1:
        ps.add(spf[a])
        a = a // spf[a]
    for p in ps:
        cnt[p] += 1
if all([x < 2 for x in cnt.values()]):
    print("pairwise coprime")
    exit()
gcd = lambda x, y : x if y == 0 else gcd(y, x % y)
x = A[0]
for a in A[1:]:
    x = gcd(x, a)
if x == 1:
    print("setwise coprime")
else:
    print("not coprime")
### BUGGY CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

S = input()
Q = INT()

for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		S = S[::-1]
	else:
		_, F, C = Que
		if F == "1":
			S = C + S
		else:
			S += C

print(S)


### FIXED CODE ###

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *
 
S = input()
Q = INT()
q = deque(S)
flg = 1
for _ in range(Q):
	Que = input().split()
	if Que[0] == "1":
		flg *= -1
	else:
		_, F, C = Que
		if any(int(F)*flg == x for x in [2, -1]):
			q.append(C)
		else:
			q.appendleft(C)
if flg == 1:
	print(*q, sep = "")
else:
	print(*list(q)[::-1], sep = "")
### BUGGY CODE ###

def sum_m(m):
    return m*(m-1)//2
S=str(input())
n=len(S)
mod=2019
ans=0
mod_set=set()
box=[0 for i in range(n)]
T=[0 for i in range(n+1)]
T[n-1]=int(S[n-1])
box[n-1]=1
for i in range(n-2,-1,-1):
    box[i]=box[i+1]*10%mod
    a=int(S[i])*box[i]+T[i+1]
    k=a%mod
    T[i]=k
    mod_set.add(k)
for i in mod_set:
    m=T.count(i)
    ans+=sum_m(m)
print(ans)


### FIXED CODE ###

S=str(input())
n=len(S)
mod=2019
ans=0
mod_set=[0]*mod
mod_set[0]+=1
ten=1
k=0
for i in range(n-1,-1,-1):
    k=(k+ten*int(S[i]))%mod
    mod_set[k]+=1
    ten=(ten*10)%mod

for i in mod_set:
    ans+=i*(i-1)//2
print(ans)
### BUGGY CODE ###

while 1:
	n = input()
	if n == 0: break
	A = map(int,raw_input().split())
	S = set(A)
	for i in S:
		if A.count(i) > n/2:
			print i
			break
	else:
		print "NO COLOR"

### FIXED CODE ###

while 1:
    n = input()
    if n == 0: break
    A = map(int,raw_input().split())
    A.sort()
    c,l = A[0],0
    for i in A:
        if i == c:
            l += 1
            if l > n/2:
                print c
                break
        else:
            c = i
            l = 1
    else:
        print "NO COLOR"
### BUGGY CODE ###

ABC = 'ABC'
S = input()

dp = [0, 0, 0, 0]
dp[3] = 1

for i in range(len(S)-1, -1, -1):
    c = S[i]
    if c == '?':
        m, m1 = 3, 3
    else:
        m, m1 = 1, 1

    for j in range(3):
        m2 = 1 if c == '?' or c == ABC[j] else 0
        dp[j] = m1 * dp[j] + m2 * dp[j+1]
    dp[3] = m * dp[3]

print(dp[0] % 1000000007)


### FIXED CODE ###

def we_love_abc(S: str)->int:
    ABC = 'ABC'
    MOD = 1000000007

    dp = [0, 0, 0, 0]
    dp[3] = 1

    for c in reversed(S):
        for j in range(3):
            m1 = 3 if c == '?' else 1
            m2 = 1 if c == '?' or c == ABC[j] else 0
            dp[j] = (m1 * dp[j] + m2 * dp[j+1]) % MOD
        dp[3] = (3 if c == '?' else 1) * dp[3]
        dp[3] %= MOD

    return dp[0] % MOD


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)
### BUGGY CODE ###

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

n,u,v = map(int, input().split())
adjacent_list = [[] for i in range(n+1)]
aoki = [0 for i in range(n+1)]
takahashi = [0 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)

def dfs(node, count,li):
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            dfs(i, count + 1,li)
def dfs_par(node, count,li):
    global node_par
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            node_par[i] = node
            dfs_par(i, count + 1,li)
        
node_par = [0 for i in range(n+1)]

finished = set()
dfs(u,0,takahashi)

#辿って親保存しとく
finished = set()
dfs_par(v,0,aoki)

#さかのぼれるとこまで行く,startがさかのぼれる最大のノーど
par = u
start = u
while takahashi[par] < aoki[par]:
    start = par
    par = node_par[start]

ans_height = [0 for i in range(n+1)]
finished = set([node_par[start]])
dfs(start,0,ans_height)
print(max(ans_height) + aoki[start] - 1)


### FIXED CODE ###

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

n,u,v = map(int, input().split())
adjacent_list = [[] for i in range(n+1)]
aoki = [0 for i in range(n+1)]
takahashi = [0 for i in range(n+1)]
for i in range(n-1):
    a,b = map(int ,input().split())
    adjacent_list[a].append(b)
    adjacent_list[b].append(a)

def dfs(node, count,li):
    global node_par
    li[node] = count
    finished.add(node)
    for i in adjacent_list[node]:
        if i not in finished:
            node_par[i] = node
            dfs(i, count + 1,li)
        
node_par = [0 for i in range(n+1)]

finished = set()
dfs(u,0,takahashi)

#辿って親保存しとく
finished = set()
dfs(v,0,aoki)

#さかのぼれるとこまで行く,startがさかのぼれる最大のノーど
par = u
start = u
while takahashi[par] < aoki[par]:
    start = par
    par = node_par[start]

ans_height = [0 for i in range(n+1)]
finished = set([node_par[start]])
dfs(start,0,ans_height)
print(max(ans_height) + aoki[start] - 1)
### BUGGY CODE ###

h,w = map(int,input().split())
al = []
for _ in range(h):
    a = list(map(int,input().split()))
    al.append(a)

bl = []
for _ in range(h):
    b = list(map(int,input().split()))
    bl.append(b)

abl = [[0 for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        abl[i][j] = al[i][j] - bl[i][j]

from collections import defaultdict
dic = defaultdict(set)

dic[(0,0)].add(abl[0][0])
dic[(0,0)].add(-abl[0][0])
for i in range(h):
    for j in range(w):
        for v in dic[(i,j)]:
            if j + 1 < w:
                if v+abl[i][j+1] <= 80:
                    dic[(i,j+1)].add(v+abl[i][j+1])
                if -80 <= v-abl[i][j+1]:
                    dic[(i,j+1)].add(v-abl[i][j+1])
            if i + 1 < h:
                if v+abl[i+1][j] <= 80:
                    dic[(i+1,j)].add(v+abl[i+1][j])
                if - 80 <= v-abl[i+1][j]:
                    dic[(i+1,j)].add(v-abl[i+1][j])

ans = float("inf")
for i in dic[(h-1, w-1)]:
    ans = min(abs(i), ans)
print(ans)




### FIXED CODE ###

h,w = map(int,input().split())

bitdp = [[0 for j in range(w)] for i in range(h)]
al = []
bl = []
for _ in range(h):
   a = list(map(int,input().split()))
   al.append(a)
for _ in range(h):
   b = list(map(int,input().split()))
   bl.append(b)
abl = [[] for i in range(h)]
for i in range(h):
   for j in range(w):
      abl[i].append(abs(al[i][j] - bl[i][j]))

bitdp[0][0] = 1 << h*w+abl[0][0]
for i in range(h):
   for j in range(w):
      if j != w-1:
         bitdp[i][j+1] |= bitdp[i][j] << abl[i][j+1] | bitdp[i][j] >> abl[i][j+1]
      if i != h-1:
         bitdp[i+1][j] |= bitdp[i][j] << abl[i+1][j] | bitdp[i][j] >> abl[i+1][j]

ans = []
for i in range(h*w*2+1):
   if (bitdp[-1][-1] >> i & 1):
      ans.append(abs(h*w-i))

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

def main():
    c11, c12, c13 = map(int, input().split())
    c21, c22, c23 = map(int, input().split())
    c31, c32, c33 = map(int, input().split())

    r1max = max([c11, c12, c13])
    for a1 in range(0, r1max + 1):
        r2max = max([c21, c22, c23])
        for a2 in range(0, r2max + 1):
            r3max = max([c31, c32, c33])
            for a3 in range(0, r3max + 1):
                m = max([a1, a2, a3])
                for b1 in range(0, 101 - m):
                    C11 = a1 + b1
                    C21 = a2 + b1
                    C31 = a3 + b1
                    if C11 == c11 and C21 == c21 and C31 == c31:
                        for b2 in range(0, 101 - m):
                            C12 = a1 + b2
                            C22 = a2 + b2
                            C32 = a3 + b2
                            if C12 == c12 and C22 == c22 and C32 == c32:
                                for b3 in range(0, 101 - m):
                                    C13 = a1 + b3
                                    C23 = a2 + b3
                                    C33 = a3 + b3
                                    if C13 == c13 and C23 == c23 and C33 == c33:
                                        print('Yes')
                                        return

    print('No')
    return


if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    c11, c12, c13 = map(int, input().split())
    c21, c22, c23 = map(int, input().split())
    c31, c32, c33 = map(int, input().split())

    r1max = max([c11, c12, c13])
    r2max = max([c21, c22, c23])
    r3max = max([c31, c32, c33])
    c1max = max([c11, c21, c31])
    c2max = max([c12, c22, c32])
    c3max = max([c13, c23, c33])
    for a1 in range(0, r1max + 1):
        for a2 in range(0, r2max + 1):
            for a3 in range(0, r3max + 1):
                m = max([a1, a2, a3])
                for b1 in range(0, c1max - m + 1):
                    C11 = a1 + b1
                    C21 = a2 + b1
                    C31 = a3 + b1
                    if C11 == c11 and C21 == c21 and C31 == c31:
                        for b2 in range(0, c2max - m + 1):
                            C12 = a1 + b2
                            C22 = a2 + b2
                            C32 = a3 + b2
                            if C12 == c12 and C22 == c22 and C32 == c32:
                                for b3 in range(0, c3max - m + 1):
                                    C13 = a1 + b3
                                    C23 = a2 + b3
                                    C33 = a3 + b3
                                    if C13 == c13 and C23 == c23 and C33 == c33:
                                        print('Yes')
                                        return

    print('No')
    return


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

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

dp = [0] + [1000000000000]*(N-1)

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

print(dp[-1])


### FIXED CODE ###

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

dp = [0] + [1000000000000]*(N-1)

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

print(dp[-1])