import itertools,sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N,K = LI()

h = LI()

dp = [float('INF')]*N

dp[0] = 0

for i,j in itertools.product(list(range(N)),list(range(1,K+1))):

    if i+j>=N:

        continue

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

print((dp[-1]))

import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))

N,K = LI()

h = LI()

dp = [float('INF')]*(N)

dp[0] = 0

for i in range(1,N):

    for j in range(max(0,i-K),i):

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

print((dp[-1]))
import itertools, sys


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = LI()
h = LI()
dp = [float("INF")] * N
dp[0] = 0
for i, j in itertools.product(list(range(N)), list(range(1, K + 1))):
    if i + j >= N:
        continue
    dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
print((dp[-1]))

import sys


def LI():
    return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = LI()
h = LI()
dp = [float("INF")] * (N)
dp[0] = 0
for i in range(1, N):
    for j in range(max(0, i - K), i):
        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))
print((dp[-1]))

-import itertools, sys +import sys -dp = [float("INF")] * N +dp = [float("INF")] * (N) -for i, j in itertools.product(list(range(N)), list(range(1, K + 1))): -    if i + j >= N: -        continue -    dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i])) +for i in range(1, N): +    for j in range(max(0, i - K), i): +        dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))