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

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

INF = 10**10

 

dp = [INF]*N

dp[0] = 0

 

for i,hi in enumerate(h):

    if i == 0:

        continue

    s = i-K if i-K >= 0 else 0

    dp[i] = min([dpk + abs(hi-hk) for dpk,hk in zip(dp[s:i],h[s:i])])

 

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

read = lambda: sys.stdin.readline().rstrip()

 

def main():

    INF = 10 ** 20

 

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

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

    dp = [INF]*(n+1)

    dp[0] = 0

 

    for i in range(1, n):

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

            if i - j < 0:

                pass

            else:

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

 

    print((dp[n-1]))

if __name__ == "__main__":

    main()
N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
INF = 10**10
dp = [INF] * N
dp[0] = 0
for i, hi in enumerate(h):
    if i == 0:
        continue
    s = i - K if i - K >= 0 else 0
    dp[i] = min([dpk + abs(hi - hk) for dpk, hk in zip(dp[s:i], h[s:i])])
print((dp[-1]))

import sys

read = lambda: sys.stdin.readline().rstrip()


def main():
    INF = 10**20
    n, k = list(map(int, input().split()))
    h = tuple(map(int, read().split()))
    dp = [INF] * (n + 1)
    dp[0] = 0
    for i in range(1, n):
        for j in range(1, k + 1):
            if i - j < 0:
                pass
            else:
                dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
    print((dp[n - 1]))


if __name__ == "__main__":
    main()

-N, K = list(map(int, input().split())) -h = list(map(int, input().split())) -INF = 10**10 -dp = [INF] * N -dp[0] = 0 -for i, hi in enumerate(h): -    if i == 0: -        continue -    s = i - K if i - K >= 0 else 0 -    dp[i] = min([dpk + abs(hi - hk) for dpk, hk in zip(dp[s:i], h[s:i])]) -print((dp[-1])) +import sys + +read = lambda: sys.stdin.readline().rstrip() + + +def main(): +    INF = 10**20 +    n, k = list(map(int, input().split())) +    h = tuple(map(int, read().split())) +    dp = [INF] * (n + 1) +    dp[0] = 0 +    for i in range(1, n): +        for j in range(1, k + 1): +            if i - j < 0: +                pass +            else: +                dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j])) +    print((dp[n - 1])) + + +if __name__ == "__main__": +    main()