#!/usr/bin/env pypy



import sys

inp = sys.stdin.readline

out = print

MAX = 10**9

 

n, k = map(int, inp().split())

cost = list(map(int, inp().split()))

dp = [MAX] * (n)

dp[0] = 0

 

for i in range(1, n):

    dp[i] = min(dp[j] + abs(cost[i] - cost[j]) for j in range(max(0, i - k), i))

 

out(dp[-1])

#!/usr/bin/env pypy

 

import sys

inp = sys.stdin.readline

out = print

MAX = 10**9

 

n, k = map(int, inp().split())

cost = list(map(int, inp().split()))

dp = [MAX] * (n)

dp[0] = 0

 

for i in range(1, n):

    for j in range(max(0, i-k), i+1):

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

 

print(dp[-1])

#!/usr/bin/env pypy
import sys

inp = sys.stdin.readline
out = print
MAX = 10**9
n, k = map(int, inp().split())
cost = list(map(int, inp().split()))
dp = [MAX] * (n)
dp[0] = 0
for i in range(1, n):
    dp[i] = min(dp[j] + abs(cost[i] - cost[j]) for j in range(max(0, i - k), i))
out(dp[-1])

#!/usr/bin/env pypy
import sys

inp = sys.stdin.readline
out = print
MAX = 10**9
n, k = map(int, inp().split())
cost = list(map(int, inp().split()))
dp = [MAX] * (n)
dp[0] = 0
for i in range(1, n):
    for j in range(max(0, i - k), i + 1):
        dp[i] = min(dp[i], dp[j] + abs(cost[i] - cost[j]))
print(dp[-1])

-    dp[i] = min(dp[j] + abs(cost[i] - cost[j]) for j in range(max(0, i - k), i)) -out(dp[-1]) +    for j in range(max(0, i - k), i + 1): +        dp[i] = min(dp[i], dp[j] + abs(cost[i] - cost[j])) +print(dp[-1])