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

*h, = list(map(int, input().split()))



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

dp[0] = 0



for i in range(1, n):

    if i < k:

        dp[i] = min([dpj+abs(hj-h[i]) for dpj, hj in zip(dp[:i], h[:i])])

    else:

        dp[i] = min([dpj+abs(hj-h[i]) for dpj, hj in zip(dp[i-k:i], h[i-k:i])])



print((dp[n-1]))

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

*h, = list(map(int, input().split()))



dp = [float("inf")]*n

dp[0] = 0



for i in range(1, n):

    if i == 1:

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

        continue



    dp[i] = min([dp[i-j] + abs(h[i-j]-h[i]) for j in range(1, k+1)])



print((dp[n-1]))

n, k = list(map(int, input().split()))
(*h,) = list(map(int, input().split()))
dp = [float("inf")] * (n + 1)
dp[0] = 0
for i in range(1, n):
    if i < k:
        dp[i] = min([dpj + abs(hj - h[i]) for dpj, hj in zip(dp[:i], h[:i])])
    else:
        dp[i] = min(
            [dpj + abs(hj - h[i]) for dpj, hj in zip(dp[i - k : i], h[i - k : i])]
        )
print((dp[n - 1]))

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

-dp = [float("inf")] * (n + 1) +dp = [float("inf")] * n -    if i < k: -        dp[i] = min([dpj + abs(hj - h[i]) for dpj, hj in zip(dp[:i], h[:i])]) -    else: -        dp[i] = min( -            [dpj + abs(hj - h[i]) for dpj, hj in zip(dp[i - k : i], h[i - k : i])] -        ) +    if i == 1: +        dp[1] = dp[0] + abs(h[0] - h[1]) +        continue +    dp[i] = min([dp[i - j] + abs(h[i - j] - h[i]) for j in range(1, k + 1)])