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



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



dp = [0 for i in range(N)]

if N >= K :    

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

        dp[i] = abs(L[i]-L[0])

    for i in range(K+1,len(dp)) :

        state = float("inf")

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

            calc = abs(L[i]-L[j]) + dp[j]

            if state > calc :

                state = calc

        dp[i] = state

    print((dp[N-1]))

else :

    print((abs(min(L[1:])-L[0])))



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

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



dp = [0 for i in range(N)]



if N < K :

    ans = abs(min(L[1:]) - L[0])

else :

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

        dp[i] = abs(L[0] - L[i])

    for i in range(K+1,N) :

        cost = float("inf")

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

            calc = dp[j] + abs(L[j] - L[i])

            if cost > calc :

                cost = calc

        dp[i] = cost



    ans = dp[N-1]



print(ans)

N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
dp = [0 for i in range(N)]
if N >= K:
    for i in range(1, K + 1):
        dp[i] = abs(L[i] - L[0])
    for i in range(K + 1, len(dp)):
        state = float("inf")
        for j in range(i - K, i):
            calc = abs(L[i] - L[j]) + dp[j]
            if state > calc:
                state = calc
        dp[i] = state
    print((dp[N - 1]))
else:
    print((abs(min(L[1:]) - L[0])))

N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
dp = [0 for i in range(N)]
if N < K:
    ans = abs(min(L[1:]) - L[0])
else:
    for i in range(1, K + 1):
        dp[i] = abs(L[0] - L[i])
    for i in range(K + 1, N):
        cost = float("inf")
        for j in range(i - K, i):
            calc = dp[j] + abs(L[j] - L[i])
            if cost > calc:
                cost = calc
        dp[i] = cost
    ans = dp[N - 1]
print(ans)

-if N >= K: +if N < K: +    ans = abs(min(L[1:]) - L[0]) +else: -        dp[i] = abs(L[i] - L[0]) -    for i in range(K + 1, len(dp)): -        state = float("inf") +        dp[i] = abs(L[0] - L[i]) +    for i in range(K + 1, N): +        cost = float("inf") -            calc = abs(L[i] - L[j]) + dp[j] -            if state > calc: -                state = calc -        dp[i] = state -    print((dp[N - 1])) -else: -    print((abs(min(L[1:]) - L[0]))) +            calc = dp[j] + abs(L[j] - L[i]) +            if cost > calc: +                cost = calc +        dp[i] = cost +    ans = dp[N - 1] +print(ans)