import sys

input = sys.stdin.readline

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

h = tuple(map(int,input().split()))



dp = [0] + [float("inf")]*(n-1)

#print(dp)



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)



print((dp[-1]))



import sys

input = sys.stdin.readline





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

h = tuple(map(int,input().split()))



def frog_2(N, H, K):

    

    dp = [0] + [float("inf")]*(n-1)

    #print(dp)

    

    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)

    return print(dp[-1])



frog_2(n,h,k)



import sys

input = sys.stdin.readline
n, k = list(map(int, input().split()))
h = tuple(map(int, input().split()))
dp = [0] + [float("inf")] * (n - 1)
# print(dp)
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)
print((dp[-1]))

import sys

input = sys.stdin.readline
n, k = map(int, input().split())
h = tuple(map(int, input().split()))


def frog_2(N, H, K):
    dp = [0] + [float("inf")] * (n - 1)
    # print(dp)
    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)
    return print(dp[-1])


frog_2(n, h, k)

-n, k = list(map(int, input().split())) +n, k = map(int, input().split()) -dp = [0] + [float("inf")] * (n - 1) -# print(dp) -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) -print((dp[-1])) + + +def frog_2(N, H, K): +    dp = [0] + [float("inf")] * (n - 1) +    # print(dp) +    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) +    return print(dp[-1]) + + +frog_2(n, h, k)