import sys

import numpy as np



input = sys.stdin.readline



n, k = list(map(int, input().split()))

h = [int(i) for i in input().split()]



dp = np.zeros(n, dtype=int)

h = np.array(h)





for i in range(1, n):

    s = max(0, i-k)

    dp[i] = min(dp[s: i] + np.abs(h[i] - h[s: i]))



print((dp[n-1]))
# B - Frog 2

n,k=list(map(int,input().split()))

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

dp=[float("inf")]*n

dp[0]=0

for i in range(n-1):

    for j in range(1,k+1):

        if i+j<n:

            dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))

print((dp[n-1]))
import sys
import numpy as np

input = sys.stdin.readline
n, k = list(map(int, input().split()))
h = [int(i) for i in input().split()]
dp = np.zeros(n, dtype=int)
h = np.array(h)
for i in range(1, n):
    s = max(0, i - k)
    dp[i] = min(dp[s:i] + np.abs(h[i] - h[s:i]))
print((dp[n - 1]))

# B - Frog 2
n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [float("inf")] * n
dp[0] = 0
for i in range(n - 1):
    for j in range(1, k + 1):
        if i + j < n:
            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
print((dp[n - 1]))

-import sys -import numpy as np - -input = sys.stdin.readline +# B - Frog 2 -h = [int(i) for i in input().split()] -dp = np.zeros(n, dtype=int) -h = np.array(h) -for i in range(1, n): -    s = max(0, i - k) -    dp[i] = min(dp[s:i] + np.abs(h[i] - h[s:i])) +h = list(map(int, input().split())) +dp = [float("inf")] * n +dp[0] = 0 +for i in range(n - 1): +    for j in range(1, k + 1): +        if i + j < n: +            dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))