# -*- coding: utf-8 -*-



import numpy as np



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

 

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

 

dp = np.zeros(N)

 

for n in range(1, N):

    k = min(K, n)

    temp = np.min(dp[n-k:n] + np.abs(h[n]-h[n-k:n]))

    dp[n] = temp

print((int(dp[-1])))
# -*- coding: utf-8 -*-

 

import numpy as np

 

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

 

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

 

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

 

for n in range(1, N):

    k = min(K, n)

    temp = np.min(dp[n-k:n] + np.abs(h[n]-h[n-k:n]))

    dp[n] = temp

print((dp[-1]))
# -*- coding: utf-8 -*-
import numpy as np

N, K = list(map(int, input().split()))
h = np.array(list(map(int, input().split())))
dp = np.zeros(N)
for n in range(1, N):
    k = min(K, n)
    temp = np.min(dp[n - k : n] + np.abs(h[n] - h[n - k : n]))
    dp[n] = temp
print((int(dp[-1])))

# -*- coding: utf-8 -*-
import numpy as np

N, K = list(map(int, input().split()))
h = np.array(list(map(int, input().split())))
dp = np.zeros(N, dtype=np.int)
for n in range(1, N):
    k = min(K, n)
    temp = np.min(dp[n - k : n] + np.abs(h[n] - h[n - k : n]))
    dp[n] = temp
print((dp[-1]))

-dp = np.zeros(N) +dp = np.zeros(N, dtype=np.int) -print((int(dp[-1]))) +print((dp[-1]))