#import numpy

[n,k] = [int(i) for i in input().split()]

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

dp = [0]

i_ls = []

now = 0

#dpのi番目の要素がi+1番目の足場までの最小コスト

for i in range(1,n):

    #i番目の足場

    if i < k:

        for j in range(i):

            i_ls.append(abs(a[i]-a[j])+dp[j])

        #print("i<k")

    else:

        for j in range(i-k,i):

            i_ls.append(abs(a[i]-a[j])+dp[j])

        #print("i>=k")

    dp.append(min(i_ls))

    #print(i_ls)

    i_ls = []



print((dp[n-1]))

[n,k] = [int(i) for i in input().split()]

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



"""

初期条件

dp[0] = 0

dp[1] = h[1] - h[0]

"""



dp = [10 ** 9 + 7] * n

dp[0] = 0

dp[1] = abs(h[1] - h[0])

if n > 2:

    for i in range(2,n):

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

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

    print((dp[n-1]))

else:

    print((dp[1]))

# import numpy
[n, k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dp = [0]
i_ls = []
now = 0
# dpのi番目の要素がi+1番目の足場までの最小コスト
for i in range(1, n):
    # i番目の足場
    if i < k:
        for j in range(i):
            i_ls.append(abs(a[i] - a[j]) + dp[j])
        # print("i<k")
    else:
        for j in range(i - k, i):
            i_ls.append(abs(a[i] - a[j]) + dp[j])
        # print("i>=k")
    dp.append(min(i_ls))
    # print(i_ls)
    i_ls = []
print((dp[n - 1]))

[n, k] = [int(i) for i in input().split()]
h = [int(i) for i in input().split()]
"""
初期条件
dp[0] = 0
dp[1] = h[1] - h[0]
"""
dp = [10**9 + 7] * n
dp[0] = 0
dp[1] = abs(h[1] - h[0])
if n > 2:
    for i in range(2, n):
        for j in range(1, min(i - 0, k) + 1):
            dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i])
    print((dp[n - 1]))
else:
    print((dp[1]))

-# import numpy -a = [int(i) for i in input().split()] -dp = [0] -i_ls = [] -now = 0 -# dpのi番目の要素がi+1番目の足場までの最小コスト -for i in range(1, n): -    # i番目の足場 -    if i < k: -        for j in range(i): -            i_ls.append(abs(a[i] - a[j]) + dp[j]) -        # print("i<k") -    else: -        for j in range(i - k, i): -            i_ls.append(abs(a[i] - a[j]) + dp[j]) -        # print("i>=k") -    dp.append(min(i_ls)) -    # print(i_ls) -    i_ls = [] -print((dp[n - 1])) +h = [int(i) for i in input().split()] +""" +初期条件 +dp[0] = 0 +dp[1] = h[1] - h[0] +""" +dp = [10**9 + 7] * n +dp[0] = 0 +dp[1] = abs(h[1] - h[0]) +if n > 2: +    for i in range(2, n): +        for j in range(1, min(i - 0, k) + 1): +            dp[i] = min(dp[i - j] + abs(h[i] - h[i - j]), dp[i]) +    print((dp[n - 1])) +else: +    print((dp[1]))