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

h = [0]+list(map(int,input().split()))

dp = [10**10]*(N+1)

dp[1] = 0

for i in range(1,N+1):

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

        if i-j>0:

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

        else:

            break

print((dp[N]))
from itertools import accumulate

from math import*

from collections import deque

from collections import defaultdict

from itertools import permutations

from heapq import*

from collections import Counter

from itertools import*

from bisect import bisect_left,bisect_right

from copy import deepcopy

inf = 10**18

from functools import reduce



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

h = [inf]*(n+k)

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

for i in range(n):

    h[i] = high[i]

dp = [inf]*(n+k)

dp[0] = 0

for i in range(n):

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

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

print((dp[n-1]))

N, K = list(map(int, input().split()))
h = [0] + list(map(int, input().split()))
dp = [10**10] * (N + 1)
dp[1] = 0
for i in range(1, N + 1):
    for j in range(1, K + 1):
        if i - j > 0:
            dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i])
        else:
            break
print((dp[N]))

from itertools import accumulate
from math import *
from collections import deque
from collections import defaultdict
from itertools import permutations
from heapq import *
from collections import Counter
from itertools import *
from bisect import bisect_left, bisect_right
from copy import deepcopy

inf = 10**18
from functools import reduce

n, k = list(map(int, input().split()))
h = [inf] * (n + k)
high = list(map(int, input().split()))
for i in range(n):
    h[i] = high[i]
dp = [inf] * (n + k)
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print((dp[n - 1]))

-N, K = list(map(int, input().split())) -h = [0] + list(map(int, input().split())) -dp = [10**10] * (N + 1) -dp[1] = 0 -for i in range(1, N + 1): -    for j in range(1, K + 1): -        if i - j > 0: -            dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]) -        else: -            break -print((dp[N])) +from itertools import accumulate +from math import * +from collections import deque +from collections import defaultdict +from itertools import permutations +from heapq import * +from collections import Counter +from itertools import * +from bisect import bisect_left, bisect_right +from copy import deepcopy + +inf = 10**18 +from functools import reduce + +n, k = list(map(int, input().split())) +h = [inf] * (n + k) +high = list(map(int, input().split())) +for i in range(n): +    h[i] = high[i] +dp = [inf] * (n + k) +dp[0] = 0 +for i in range(n): +    for j in range(1, k + 1): +        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j])) +print((dp[n - 1]))