def main():

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

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



    dp = [float('inf')] * N

    dp[0] = 0

    for i in range(1,len(A)):

        jmp = max(0,i-K)

        dp[i] = min([dp[j]+abs(A[i]-A[j]) for j in range(jmp, i)])

    print((dp[N-1]))



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

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

 

INF = 10**9

dp = [INF]*N

 

for i in range(len(A)):

    if i == 0:

        dp[0] = 0

    elif i == 1:

        dp[1] = abs(A[1]-A[0])

    else:

        jmp = min(i,K)

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

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

            

print((dp[N-1]))
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    dp = [float("inf")] * N
    dp[0] = 0
    for i in range(1, len(A)):
        jmp = max(0, i - K)
        dp[i] = min([dp[j] + abs(A[i] - A[j]) for j in range(jmp, i)])
    print((dp[N - 1]))


main()

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
INF = 10**9
dp = [INF] * N
for i in range(len(A)):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(A[1] - A[0])
    else:
        jmp = min(i, K)
        for j in range(1, jmp + 1):
            dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j]))
print((dp[N - 1]))

-def main(): -    N, K = list(map(int, input().split())) -    A = list(map(int, input().split())) -    dp = [float("inf")] * N -    dp[0] = 0 -    for i in range(1, len(A)): -        jmp = max(0, i - K) -        dp[i] = min([dp[j] + abs(A[i] - A[j]) for j in range(jmp, i)]) -    print((dp[N - 1])) - - -main() +N, K = list(map(int, input().split())) +A = list(map(int, input().split())) +INF = 10**9 +dp = [INF] * N +for i in range(len(A)): +    if i == 0: +        dp[0] = 0 +    elif i == 1: +        dp[1] = abs(A[1] - A[0]) +    else: +        jmp = min(i, K) +        for j in range(1, jmp + 1): +            dp[i] = min(dp[i], dp[i - j] + abs(A[i] - A[i - j])) +print((dp[N - 1]))