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

N = int(input())

l = list(range(N))

for n in range(N):
    l[n] = int(input())

# print(l)

count_l = []

for k in range(N):
    # print("k = {}".format(k))
    count = 1
    st_num = l[k]

    for num in l[k+1:N]:
        # print("num = {}".format(num))
        if num == st_num + 1:
            # print(num)
            count += 1
            st_num += 1

    count_l.append(count)

# print(count_l)

print(N - max(count_l))

### FIXED CODE ###

N = int(input())

l = list(range(N))

for n in range(N):
    l[n] = int(input())

# print(l)

count_l = []
ind_list = [0] * N

for i, k in enumerate(l):
    ind_list[k-1] = i

count = 1
for i in range(N):
    if i == N - 1:
        count_l.append(count)
    else:
        if ind_list[i] < ind_list[i+1]:
            count += 1
        else:
            count_l.append(count)
            count = 1

print(N - max(count_l))
### BUGGY CODE ###

#D
#入力
n,a,b=map(int,input().split())

#処理
import math

from operator import mul
from functools import reduce

def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

print((2**n-combinations_count(n, a)-combinations_count(n, b)-1)%(10**9+7))

### FIXED CODE ###

# D
#入力
n,a,b=map(int,input().split())

#諸関数
def pront(X):
    #デバッグ用プリント
    #環境変数としてdebugを確保して、それが1なら内容を出力
    if debug==0:
        print(X)

#デバッグ用
debug=1#0:本番,1:デバッグ中

#処理
#定数
base=(10**9)+7

#関数
    #10*9+7で割ったあまりを返し続ける
def add(a,b):#a+b
    return(((a%base)+(b%base))%base)
def sub(a,b):#a-b
    return (((a%base)-(b%base))%base)
def multi(a,b):#a*b
    return(((a%base)*(b%base))%base)
def exp(a,b):#a**b
    x=1
    c=a
    d=b
    while d!=0:
        if d%2==1:
            x=multi(x,c)
        c=(c**2)%base
        d=d//2
    return x

#割り算はフェルマーの小定理に基づいて考える
def div(a,b):#a/b
    return(multi(a,exp(b,base-2)))

    #その他演算
def fact(m,n):#m~nの積(m<n)
    x=1
    for i in range(m,n+1):
        x=multi(x,i)
    return x
def comb(n,r):
    return div(fact(n-r+1,n),fact(1,r))
#処理
Z=sub(sub(sub(exp(2,n),comb(n,a)),comb(n,b)),1)
print(Z)
### BUGGY CODE ###

N = int(input())
S = list(input())
S_set = set()
i_set = set()

ans_list = {}


for i in range(N):
    if S[i] in i_set:
        continue
    i_set.add(S[i])
    for j in range(i+1, N):
        if not S[j + 1:N] or S[i] + S[j] in S_set:
            continue
        ans_list[S[i] + S[j]] = len(set(S[j + 1:N]))
        S_set.add(S[i] + S[j])
count = 0
# print(ans_list)
# ans_len_list = [len(set(value)) for value in ans_list.values()]
print(sum(ans_list.values()))

### FIXED CODE ###

N = int(input())
S = list(input())
visited = {}

count = 0

for i, s in enumerate(S):
    if s in visited:
        continue
    visited[s] = []
    for j, s2 in enumerate(S[i+1:N-1]):
        # print(s, s2, S[i + j + 2:N])
        if s2 in visited[s]:
            continue
        count += len(set(S[i + j + 2:N]))
        visited[s].append(s2)
        # print(s, s2, set(S[j + 1:N]))
print(count)
### BUGGY CODE ###

import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
n,q=NM()
c=L()
No=2
while No<=n:
    No*=2
tree=[set() for i in range(2*No)]

for i in range(No-1,No-1+n):
    tree[i].add(c[i-No+1])
for i in range(No-2,-1,-1):
    tree[i]=tree[i*2+1]|tree[i*2+2]

def query(l,r):
    L=l+No-1
    R=r+No-1
    s=set()
    while L<=R:
        if R&1:
            s|=tree[R]
            R-=2
        else:
            R-=1
        if L&1:
            L-=1
        else:
            s|=tree[L]
        L>>=1;R>>=1
    return s
for i in range(q):
    l,r=NM()
    l-=1
    r-=1
    print(len(query(l,r)))

### FIXED CODE ###

import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
n,q=NM()
c=[0]+L()
Q=[L()+[i] for i in range(q)]
Q.sort(key=lambda x:x[1])
ans=[0]*(q)
lastapp=[0 for i in range(n+1)]

No=2
while No<=n:
    No*=2
tree=[0]*(2*No)

def update(k,x):
    k+=No-1
    tree[k]=x
    while k>0:
        k=(k-1)//2
        tree[k]=tree[k*2+1]+tree[k*2+2]

def add(k,x):
    k+=No-1
    while k>=0:
        tree[k]+=x
        k=(k-1)//2

def query(l,r):
    L=l+No-1
    R=r+No-1
    s=0
    while L<=R:
        if R&1:
            s+=tree[R]
            R-=2
        else:
            R-=1
        if L&1:
            L-=1
        else:
            s+=tree[L]
        L>>=1;R>>=1
    return s
j=1
for l,r,i in Q:
    while j<=r:
        t=c[j]
        add(lastapp[t],-1)
        add(j,1)
        lastapp[t]=j
        j+=1
    ans[i]=query(l,r)
for i in ans:
    print(i)
### BUGGY CODE ###

from collections import deque
import sys
sys.setrecursionlimit(10**7)

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

A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

dp = [[0] * (M+2) for _ in range(N+2)]

dp[0][0] = 0

max_c = 0
for i in range(N):
  for j in range(M):
    dp[i][j+1] = dp[i][j] + B[j]
    dp[i+1][j] = dp[i][j] + A[i]
    dp[i+1][j+1] = dp[i][j] + A[i] + B[j]
    if dp[i+1][j] <= K or dp[i][j+1] <= K:
      max_c = i+j+1
    if dp[i+1][j+1] <= K:
      max_c = i+j+2

print(max_c)

### FIXED CODE ###

#!/usr/bin/env python3

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

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

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

for i in range(M):
    b.append(b[i] + B[i])


count = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    count = max(count, i+j)

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

import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for r in rgb["R"]:
	for g in rgb["G"]:
		if 2*max(r,g)-min(r,g) in rgb["B"]:
			s+=1
for r in rgb["R"]:
	for g in rgb["B"]:
		if 2*max(r,g)-min(r,g) in rgb["G"]:
			s+=1
for r in rgb["B"]:
	for g in rgb["G"]:
		if 2*max(r,g)-min(r,g) in rgb["R"]:
			s+=1

ans=c-s
print(ans)


### FIXED CODE ###

import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=q
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def binary_search(li,item):#二分探索
	left,right = 0, len(li)
	while left<=right:
		mid = (left+right)//2
		if li[mid]==item:
			return mid
		elif li[mid]<item:
			left = mid+1
		else:
			right = mid-1
	return -1

N = readInt()
S = readChar()
rgb = {"R":[],"G":[],"B":[]}
for i in range(N):
	rgb[S[i]].append(i)

c=s=0
c=len(rgb["R"])*len(rgb["G"])*len(rgb["B"])
for i in range(N):
	for j in range(i+1,N):
		k = 2*j-i
		if k>=N:
			continue
		if S[i]!=S[j] and S[i]!=S[k] and S[k]!=S[j]:
			s+=1

ans=c-s
print(ans)
### BUGGY CODE ###

import math
n=int(input())
a=list(map(int,input().split(' ')))
l={}
m=0
for i in a:
  l[i]=l.get(i,0)+1
  if i > m:
    m = i
fl={}
for i in range(1, m+1):
  try:
    fl[i]=math.factorial(l[i])//(math.factorial(l[i]-2)*2)
  except:
    fl[i]=0
s=sum(fl.values())
for i in a:
  buf = fl[i]
  if l[i]>=3:
    c = math.factorial(l[i]-1)//(math.factorial(l[i]-3)*2)
  else:
    c = 0
  print(s-fl[i]+c)

### FIXED CODE ###

import math
n=int(input())
a=list(map(int,input().split(' ')))
l={}
fl={}
for i in a:
  l[i]=l.get(i,0)+1
  fl[i]=(l.get(i,0)*(l.get(i,0)-1))//2
s=sum(fl.values())
for i in a:
  buf = (l[i]-1)*(l[i]-2)//2
  print(s-fl[i]+buf)
### BUGGY CODE ###

n=int(input())
a=[0]*n
for i in range(n):
    a[i]=int(input())
iro=[0]
iro[0]=a[0]

for i in range(1,n):

    flag=True
    for j in range(len(iro)-1):
        if a[i]>iro[j] and a[i]<=iro[j+1]:
            iro[j]=a[i]
            flag=False
    if a[i]>iro[-1]:
        iro[-1]=a[i]
        flag=False
    if flag:
        iro.append(a[i])
#print(iro)
print(len(iro))


### FIXED CODE ###

from bisect import bisect as br
n=int(input())
x=[-1]*n
for _ in range(n):
    a=int(input())
    i=br(x,a-1)-1
    x[i]=a
print(n-x.count(-1))
### BUGGY CODE ###

n = int(input())
a = [float(input()) for _ in range(n)]
p = [0] * n
for i in range(n):
    for j in range(i + 1, n):
        if a[i] > a[j]:
            p[i] += 3
        elif a[i] == a[j]:
            p[i] += 1
            p[j] += 1
        else:
            p[j] += 3
for i in p:
    print(i)


### FIXED CODE ###

import bisect
n = int(input())
a = [float(input()) for _ in range(n)]
b = sorted(a)
for i in range(n):
    x = bisect.bisect_left(b, a[i])
    y = bisect.bisect_right(b, a[i])
    print(x * 2 + y - 1)
### BUGGY CODE ###

from itertools import combinations
N = int(input())
S = str(input())

print(len(set(combinations(S, 3))))

### FIXED CODE ###

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

ans = 0
for i in range(1000):
    n = "{:03}".format(i)
    index = 0
    c = 0
    for k in n:
        for j in range(index, N):
            if S[j] == k:
                index = j+1
                c+=1
                break
    if c == 3:
        ans += 1
print(ans)
### BUGGY CODE ###

n,k = (int(x) for x in input().split())
h = list(map(int,input().split()))
dp = [0]*n

# i = 1の時
dp[1] = abs(h[1] - h[0])

# i番目までの必要な体力を求める
for i in range(2,n):

    # i >= 2の時
    # i番目に必要な体力の最小値を求める
    dp[i] = min(dp[j] + abs(h[i] - h[j]) for j in range(max(0,i-k),i))

print(dp[-1])
                  



### FIXED CODE ###

def main():
    n,k = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0]*n
    dp[1] = abs(h[1] - h[0])
    for i in range(2,n):
        dp[i] = min(abs(h[i] - h[j]) + dp[j] for j in range(max(0,i-k),i))
    print(dp[-1])

main()
### BUGGY CODE ###

import math
N = int(input())
#for i in range(1,N+1):
    #print(sum(1 for x in range(1,int(math.sqrt(i))) for y in range(1,int(math.sqrt(i))) for z in range(1,int(math.sqrt(i))) if x*(x+y+z)+y*(y+z)+z*z==i))
for i in range(1,N+1):
    count=0
    for x in range(1,int(math.sqrt(i))):
        for y in range(1,int(math.sqrt(i))):
            for z in range(1,int(math.sqrt(i))):
                if x*(x+y+z)+y*(y+z)+z*z>i:
                    break
                    
                if x*(x+y+z)+y*(y+z)+z*z==i:
                    count+=1
    print(count) 

### FIXED CODE ###

a=[0]*int(input())
r=range(1,99)
for x in r:
    for y in r:
        for z in r:
            n=x*(x+y+z)+y*(y+z)+z*z-1
            if n<len(a):
                a[n]+=1
print(*a)
### BUGGY CODE ###

S = input()
ans = 0
while True:
    if S.count("01") == 0 and S.count("10") == 0:
        break

    if S.count("01") > 0:
        ans += S.count("01")*2
        S = S.replace("01", "")

    if S.count("10") > 0:
        ans += S.count("10")*2
        S = S.replace("10", "")

print(ans)


### FIXED CODE ###

S = input()
print(min(S.count("0"),S.count("1"))*2)
### BUGGY CODE ###

cnt=0
def merge(A,left,mid,right):
    global cnt
    n1= mid-left
    n2=right-mid
    L=[0 for i in range(n1+1)]
    R=[0 for i in range(n2+1)]
    for i in range(n1):
        L[i]=A[left+i]
    for i in range(n2):
        R[i]=A[mid+i]
    L[n1]=float("inf")
    R[n2]=float("inf")
    i=0
    j=0
    for k in range(left,right):
        if L[i]<=R[j]:
            A[k]=L[i]
            i=i+1
            cnt=cnt+1
        else:
            A[k]=R[j]
            j=j+1
            cnt=cnt+1
def mergeSort(A,left,right):
    if left+1<right:
        mid=(int)((left+right)/2)
        mergeSort(A,left,mid)
        mergeSort(A,mid,right)
        merge(A,left,mid,right)
n=int(input())
S=list(map(int,input().split(' ')))
mergeSort(S,0,len(S))
print(*S)            
print(cnt)   



### FIXED CODE ###

def merge(A,left,mid,right):
    n1=mid-left
    n2=right-mid
    L = A[left:mid]
    R = A[mid:right]
    L.append(float("inf"))
    R.append(float("inf"))
    """
    L=[0]*(n1+1)
    R=[0]*(n2+1)
    for i in range(n1):
        L[i]=A[left+i]
    for i in range(n2):
        R[i]=A[mid+i]
    L[n1]=float("inf")
    R[n2]=float("inf")
    """
    i=0
    j=0
    for k in range(left,right):
        global cnt
        cnt+=1
        if L[i]<=R[j]:
            A[k]=L[i]
            i=i+1
        else:
            A[k]=R[j]
            j=j+1

def mergeSort(A,left,right):
    if left+1<right:
        mid=(left+right)//2
        mergeSort(A,left,mid)
        mergeSort(A,mid,right)
        merge(A,left,mid,right)

n=int(input())
S=list(input().split())
for i in range(n):
    S[i]=int(S[i])
cnt=0
mergeSort(S,0,n)
for i in range(n):
    if i==n-1:
        print(S[i])
    else:
        print(S[i],end=" ")
print(cnt)
### BUGGY CODE ###

s = input()
dp = [[0 for i in range(13)] for i in range(len(s))]
if s[-1] != "?":
    dp[0][int(s[-1]) % 13] += 1
else:
    for k in range(10):
        dp[0][k % 13] += 1
d = [1]
for i in range(1,len(s)):
    d.append(d[i-1]*10 % 13)
    
for i in range(1,len(s)):
    for r in range(13):
        if s[-(i+1)] == "?":
            for k in range(10): 
                dp[i][(k * d[i] + r) % 13] += dp[i-1][r]
                
        else:
            dp[i][(int(s[-(i+1)]) * d[i] + r) % 13] += dp[i-1][r]
    for r in range(13):
        dp[i][r]  %= 10**9 + 7
print(dp[len(s)-1][5]%(10**9+7))


### FIXED CODE ###

s = input()
dp = [[0 for i in range(13)]]
if s[-1] != "?":
    dp[0][int(s[-1]) % 13] += 1
else:
    for k in range(10):
        dp[0][k % 13] += 1
        
d = [1]   
for i in range(1,len(s)):
    dp.append([0]*13)
    d.append(d[i-1]*10 % 13)
    for r in range(13):
        if s[-(i+1)] == "?":
            for k in range(10): 
                dp[i][(k * d[i] + r) % 13] += dp[i-1][r] % (10**9 + 7)
                
        else:
            dp[i][(int(s[-(i+1)]) * d[i] + r) % 13] += dp[i-1][r] % (10**9 + 7)

print(dp[len(s)-1][5]%(10**9+7))
### BUGGY CODE ###

import sys
S = sys.stdin.readline()
X = S
while True:
    new_S = S.replace("01","").replace("10","")
    if S == new_S:
        break
    S = new_S
print(len(X) - len(S))

### FIXED CODE ###

import sys
from collections import Counter
S = input()
ct = Counter(list(S))
if len(ct.keys()) == 1:
    print(0)
else:
    print(min(ct.values()) * 2)
### BUGGY CODE ###

n = int(input())

print(sum([i for i in range(n)]))

### FIXED CODE ###

n = int(input())

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

N, M, Q = list(map(int, input().split()))
L_R = []
p_q = []

for _ in range(M):
    L, R = list(map(int, input().split()))
    L_R.append((L, R))
for _ in range(Q):
    p, q = list(map(int, input().split()))
    p_q.append((p, q))

for i in p_q:
    count = 0
    for j in L_R:
        if(i[0] <= j[0] and j[1] <= i[1]):
            count += 1
    print(count)


### FIXED CODE ###

import numpy as np
N, M, Q = map(int, input().split())
X = np.zeros((N, N), dtype=int)
ends = np.zeros((M, 2), dtype=int)
queries = np.zeros((Q, 2), dtype=int)
for i in range(M):
    ends[i] = [int(item) for item in input().split()]
for i in range(Q):
    queries[i] = [int(item) for item in input().split()]
np.add.at(X, [ends[:,0] - 1, ends[:, 1] - 1], 1)
B = np.cumsum(X[::-1,:], axis = 0)[::-1,:]
C = np.cumsum(B, axis = 1)
for item in queries:
    print(C[item[0] - 1, item[1] - 1])
### BUGGY CODE ###

from collections import deque

def dfs(graph, start, goal):
    stack = deque()
    push, pop = stack.append, stack.pop
    route = [[] for i in range(N+1)]
    push([start, []])
    route[start] = [start]
    while stack:
        a, r = pop()
        for b in graph[a].keys():
            if route[b] == []:
                push([b, r+[b]])
                route[b] = r+[b]
    return route

N = int(input())
graph = {}
for i in range(N-1):
    a,b = map(int, input().split())
    if a not in graph:
        graph[a] = {}
    if b not in graph:
        graph[b] = {}
    graph[a][b] = graph[b][a] = 1
route = dfs(graph, 1, N)
# print(route)
q_len = len(route[N])
q = route[N][q_len // 2]
nex = route[N][q_len // 2]
tmp = [r for r in route if nex in r]
s_get = len(tmp)
# print(s_get, nex, N-s_get)
print('Fennec' if N - s_get > s_get else 'Snuke')

### FIXED CODE ###

from collections import deque

def dfs(graph, start, goal):
    s_stack = deque()
    g_stack = deque()
    visited = [0 for i in range(N+1)]
    s_stack.append([start,0])
    g_stack.append([goal, 0])
    visited[start] = 1 
    visited[goal] = 1 
    s_cnt = 1
    g_cnt = 1
    round = 0
    while s_stack or g_stack:
        while s_stack:
            a,cost = s_stack.popleft()
            if cost > round:
                s_stack.appendleft([a,cost])
                break
            for b in graph[a].keys():
                if not visited[b]:
                    s_stack.append([b,cost+1])
                    visited[b] += 1 
                    s_cnt += 1
        while g_stack:
            a, cost = g_stack.popleft()
            if cost > round:
                g_stack.appendleft([a,cost])
                break
            for b in graph[a].keys():
                if not visited[b]:
                    g_stack.append([b,cost+1])
                    visited[b] += 1 
                    g_cnt += 1
        round += 1
    # print(s_cnt, g_cnt)
    return s_cnt > g_cnt

N = int(input())
graph = {}
for i in range(N-1):
    a,b = map(int, input().split())
    if a not in graph:
        graph[a] = {}
    if b not in graph:
        graph[b] = {}
    graph[a][b] = graph[b][a] = 1
print('Fennec' if dfs(graph,1,N) else 'Snuke')
### BUGGY CODE ###

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

e_table = [0]
pre = 0
for i in range(1,1001):
    e_table.append((pre + i)/i)
    pre = pre + i

lis = [0] * K

maximam = 0

for p in input().split():
    p = int(p)
    lis = lis[1:]
    lis.append(e_table[p])
    maximam = max(maximam, sum(lis))
print(maximam)

### FIXED CODE ###

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

e_table = [0]
pre = 0
for i in range(1,1001):
    e_table.append((pre + i)/i)
    pre = pre + i

lis = [0] * K
SUM = 0
maximam = 0

for i, p in enumerate(input().split()):
    p = int(p)
    lis.append(e_table[p])
    SUM = SUM - lis[i] + lis[-1]
    maximam = max(maximam, SUM)
print(maximam)
### BUGGY CODE ###

h,w = map(int,input().split())
s = [[i for i in input()] for j in range(h)]


"""
s_idx = [-1,-1]
g_idx = [-1,-1]

for i in range(h):
    for j in range(w):
        if s[i][j] == 'S':
            s_idx[0] = i
            s_idx[1] = j
        if s[i][j] == 'G':
            g_idx[0] = i
            g_idx[1] = j
"""

tate = [0,0,1,-1]
yoko = [-1,1,0,0]

mat=[[10**18 for i in range(w*h)] for j in range(w*h)]

for i in range(w*h):
	mat[i][i]=0

for i in range(w*h):
    for j in range(4):
        next_idx = i + w*tate[j] + yoko[j]
        if next_idx < 0 or next_idx >= w*h:
            continue
        if i % w == 0:
            if j == 0:
                continue
        if i % w == w - 1:
            if j == 1:
                continue
        if s[next_idx//w][next_idx%w] == '#':
            mat[i][next_idx] = 10**18
        else:
            mat[i][next_idx] = 1

for i in range(w*h):
    #print(i,i,i,i,i,i,i,i,i)
    for x in range(w*h):
        for y in range(w*h):
            """
            if x == s_idx[0]*w+s_idx[1] and y == g_idx[0]*w+g_idx[1]:
                print(mat[x][y],mat[x][i] , mat[i][y],i)
            """
            mat[x][y] = min(mat[x][y], mat[x][i] + mat[i][y])
    
ans = 5000
tmp = []

for i in range(w*h):
    for j in range(w*h):
        if ans > mat[i][j]:
            tmp.append(mat[i][j])
            
print(max(tmp))



### FIXED CODE ###

h,w = map(int,input().split())
s = [[i for i in input()] for j in range(h)]


"""
s_idx = [-1,-1]
g_idx = [-1,-1]

for i in range(h):
    for j in range(w):
        if s[i][j] == 'S':
            s_idx[0] = i
            s_idx[1] = j
        if s[i][j] == 'G':
            g_idx[0] = i
            g_idx[1] = j
"""

tate = [0,0,1,-1]
yoko = [-1,1,0,0]

mat=[[10**18 for i in range(w*h)] for j in range(w*h)]

for i in range(w*h):
    mat[i][i] = 0

for i in range(w*h):
    for j in range(4):
        next_idx = i + w*tate[j] + yoko[j]
        if next_idx < 0 or next_idx >= w*h:
            continue
        if i % w == 0:
            if j == 0:
                continue
        if i % w == w - 1:
            if j == 1:
                continue
        if s[next_idx//w][next_idx%w] == '#':
            mat[i][next_idx] = 10**18
        else:
            mat[i][next_idx] = 1

for i in range(w*h):
    #print(i,i,i,i,i,i,i,i,i)
    for x in range(w*h):
        for y in range(w*h):
            """
            if x == s_idx[0]*w+s_idx[1] and y == g_idx[0]*w+g_idx[1]:
                print(mat[x][y],mat[x][i] , mat[i][y],i)
            """
            mat[x][y] = min(mat[x][y], mat[x][i] + mat[i][y])
    
ans = 10**18
tmp = []

for i in range(w*h):
    for j in range(w*h):
        if s[i//w][i%w] == '#':
            continue
        if s[j//w][j%w] == '#':
            continue
        if ans > mat[i][j]:
            tmp.append(mat[i][j])
            
print(max(tmp))
### BUGGY CODE ###

import numpy as np
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
h = np.array([int(x) for x in input().split()])
dp = np.zeros(N)
dp[0] = 0
for i in range(1, N):
  j = i - min(i, K)
  dp[i] = min(dp[j:i]+np.abs(h[i]-h[j:i]))
print(int(dp[N-1]))

### FIXED CODE ###

import sys
import numpy as np

# inputを高速化する。
input = sys.stdin.readline

n, k = map(int, input().split())
h = [int(i) for i in input().split()]

# dpの配列を作る。
dp = np.zeros(n, dtype=int)
# hの配列を作る。
h = np.array(h)


for i in range(1, n):
    # start は負になることはない。
    start = max(0, i-k)
    # 行列として足し算できるのでforを使う必要がない。
    dp[i] = min(dp[start: i] + np.abs(h[i] - h[start: i]))

print(dp[n-1])