import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



N,K,*H = list(map(int,read().split()))



H = [H[0]]*K+H



dp = [0]*(K+N)



for n in range(K,N+K):

    h = H[n]

    dp[n] = min(x+abs(y-h) for x,y in zip(dp[n-K:n],H[n-K:n]))



answer = dp[-1]

print(answer)
import sys

import numpy as np



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



def main(H, K):

    N = len(H)

    INF = 10**9

    dp = np.full(N + K, INF, np.int32)

    dp[0] = 0

    for n in range(N):

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

            dp[n + k] = min(dp[n + k], dp[n] + abs(H[n] - H[n + k]))

    return dp[N - 1]



signature = '(i4[:],i4)'

if sys.argv[-1] == 'ONLINE_JUDGE':

    from numba.pycc import CC

    cc = CC('my_module')

    cc.export('main', signature)(main)

    cc.compile()



from my_module import main



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

H = np.array(read().split(), np.int32)



print((main(H, K)))
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K, *H = list(map(int, read().split()))
H = [H[0]] * K + H
dp = [0] * (K + N)
for n in range(K, N + K):
    h = H[n]
    dp[n] = min(x + abs(y - h) for x, y in zip(dp[n - K : n], H[n - K : n]))
answer = dp[-1]
print(answer)

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main(H, K):
    N = len(H)
    INF = 10**9
    dp = np.full(N + K, INF, np.int32)
    dp[0] = 0
    for n in range(N):
        for k in range(1, K + 1):
            dp[n + k] = min(dp[n + k], dp[n] + abs(H[n] - H[n + k]))
    return dp[N - 1]


signature = "(i4[:],i4)"
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("main", signature)(main)
    cc.compile()
from my_module import main

N, K = list(map(int, readline().split()))
H = np.array(read().split(), np.int32)
print((main(H, K)))

+import numpy as np -N, K, *H = list(map(int, read().split())) -H = [H[0]] * K + H -dp = [0] * (K + N) -for n in range(K, N + K): -    h = H[n] -    dp[n] = min(x + abs(y - h) for x, y in zip(dp[n - K : n], H[n - K : n])) -answer = dp[-1] -print(answer) + + +def main(H, K): +    N = len(H) +    INF = 10**9 +    dp = np.full(N + K, INF, np.int32) +    dp[0] = 0 +    for n in range(N): +        for k in range(1, K + 1): +            dp[n + k] = min(dp[n + k], dp[n] + abs(H[n] - H[n + k])) +    return dp[N - 1] + + +signature = "(i4[:],i4)" +if sys.argv[-1] == "ONLINE_JUDGE": +    from numba.pycc import CC + +    cc = CC("my_module") +    cc.export("main", signature)(main) +    cc.compile() +from my_module import main + +N, K = list(map(int, readline().split())) +H = np.array(read().split(), np.int32) +print((main(H, K)))