import sys

def main():

    INF=sys.maxsize

    N,K=tuple(map(int,sys.stdin.readline().split()))

    h=tuple(map(int,sys.stdin.readline().split()))

    dp=[INF for _ in range(N)]

    dp[0]=0

    for i in range(N-1):

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

            if i+j<N:dp[i+j]=min([dp[i+j],dp[i]+abs(h[i+j]-h[i])])

    print((dp[N-1]))

if __name__=='__main__':main()
import sys

def main():

    INF=sys.maxsize

    N,K=tuple(map(int,sys.stdin.readline().split()))

    h=tuple(map(int,sys.stdin.readline().split()))

    dp=[INF for _ in range(N)]

    dp[0]=0

    for i in range(1,N):

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

            if i-j>=0:

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

                if dij<dp[i]:dp[i] = dij

    print((dp[N-1]))

if __name__=='__main__':main()
import sys


def main():
    INF = sys.maxsize
    N, K = tuple(map(int, sys.stdin.readline().split()))
    h = tuple(map(int, sys.stdin.readline().split()))
    dp = [INF for _ in range(N)]
    dp[0] = 0
    for i in range(N - 1):
        for j in range(1, K + 1):
            if i + j < N:
                dp[i + j] = min([dp[i + j], dp[i] + abs(h[i + j] - h[i])])
    print((dp[N - 1]))


if __name__ == "__main__":
    main()

import sys


def main():
    INF = sys.maxsize
    N, K = tuple(map(int, sys.stdin.readline().split()))
    h = tuple(map(int, sys.stdin.readline().split()))
    dp = [INF for _ in range(N)]
    dp[0] = 0
    for i in range(1, N):
        for j in range(1, K + 1):
            if i - j >= 0:
                dij = dp[i - j] + abs(h[i] - h[i - j])
                if dij < dp[i]:
                    dp[i] = dij
    print((dp[N - 1]))


if __name__ == "__main__":
    main()

-    for i in range(N - 1): +    for i in range(1, N): -            if i + j < N: -                dp[i + j] = min([dp[i + j], dp[i] + abs(h[i + j] - h[i])]) +            if i - j >= 0: +                dij = dp[i - j] + abs(h[i] - h[i - j]) +                if dij < dp[i]: +                    dp[i] = dij