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

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

inf = 10 ** 4 + 1

dp = [inf] * N

dp[0] = 0

for i in range(1,N):

    H = h[i]

    dp[i] = min( [dp[j] + abs(h[j] - H) for j in range(max(0, i-K),i)])

print((dp[-1]))

import sys

def main():

    input=sys.stdin.readline

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

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

    if N-1<=K:

        print((H[N-1]-H[0]))

        return

    inf=10**9+7

    dp=[inf]*N

    dp[0]=0



    for i,h in enumerate(H):

        if i==0:continue

        m=inf

        M=max(0,i-K)

        dp[i]=min([dpt+abs(h-ht) for ht, dpt in zip(H[M:i],dp[M:i])])

    print((dp[N-1]))

    return

if __name__=="__main__":

    main()

N, K = list(map(int, input().split()))
h = list(map(int, input().split()))
inf = 10**4 + 1
dp = [inf] * N
dp[0] = 0
for i in range(1, N):
    H = h[i]
    dp[i] = min([dp[j] + abs(h[j] - H) for j in range(max(0, i - K), i)])
print((dp[-1]))

import sys


def main():
    input = sys.stdin.readline
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))
    if N - 1 <= K:
        print((H[N - 1] - H[0]))
        return
    inf = 10**9 + 7
    dp = [inf] * N
    dp[0] = 0
    for i, h in enumerate(H):
        if i == 0:
            continue
        m = inf
        M = max(0, i - K)
        dp[i] = min([dpt + abs(h - ht) for ht, dpt in zip(H[M:i], dp[M:i])])
    print((dp[N - 1]))
    return


if __name__ == "__main__":
    main()

-N, K = list(map(int, input().split())) -h = list(map(int, input().split())) -inf = 10**4 + 1 -dp = [inf] * N -dp[0] = 0 -for i in range(1, N): -    H = h[i] -    dp[i] = min([dp[j] + abs(h[j] - H) for j in range(max(0, i - K), i)]) -print((dp[-1])) +import sys + + +def main(): +    input = sys.stdin.readline +    N, K = list(map(int, input().split())) +    H = list(map(int, input().split())) +    if N - 1 <= K: +        print((H[N - 1] - H[0])) +        return +    inf = 10**9 + 7 +    dp = [inf] * N +    dp[0] = 0 +    for i, h in enumerate(H): +        if i == 0: +            continue +        m = inf +        M = max(0, i - K) +        dp[i] = min([dpt + abs(h - ht) for ht, dpt in zip(H[M:i], dp[M:i])]) +    print((dp[N - 1])) +    return + + +if __name__ == "__main__": +    main()