import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



import numpy as np



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

H = np.array(input().split(), dtype=np.int64)



N,K,H



dp = np.zeros(N,dtype=np.int64)

for i in range(1,N):

    L = max(0,i-K)

    dp[i] = np.min(np.abs(H[i] - H[L:i]) + dp[L:i])



answer = dp[-1]

print(answer)
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



N,K,*H = list(map(int,read().split()))



H = [H[0]]*K+H



dp = [0]*(K+N)



for n in range(K,N+K):

    h = H[n]

    dp[n] = min(x+abs(y-h) for x,y in zip(dp[n-K:n],H[n-K:n]))



answer = dp[-1]

print(answer)
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
import numpy as np

N, K = list(map(int, input().split()))
H = np.array(input().split(), dtype=np.int64)
N, K, H
dp = np.zeros(N, dtype=np.int64)
for i in range(1, N):
    L = max(0, i - K)
    dp[i] = np.min(np.abs(H[i] - H[L:i]) + dp[L:i])
answer = dp[-1]
print(answer)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K, *H = list(map(int, read().split()))
H = [H[0]] * K + H
dp = [0] * (K + N)
for n in range(K, N + K):
    h = H[n]
    dp[n] = min(x + abs(y - h) for x, y in zip(dp[n - K : n], H[n - K : n]))
answer = dp[-1]
print(answer)

-input = sys.stdin.readline -sys.setrecursionlimit(10**7) -import numpy as np - -N, K = list(map(int, input().split())) -H = np.array(input().split(), dtype=np.int64) -N, K, H -dp = np.zeros(N, dtype=np.int64) -for i in range(1, N): -    L = max(0, i - K) -    dp[i] = np.min(np.abs(H[i] - H[L:i]) + dp[L:i]) +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +N, K, *H = list(map(int, read().split())) +H = [H[0]] * K + H +dp = [0] * (K + N) +for n in range(K, N + K): +    h = H[n] +    dp[n] = min(x + abs(y - h) for x, y in zip(dp[n - K : n], H[n - K : n]))