def a():

    N, K = [int(n) for n in input().split()]

    h = [int(n) for n in input().split()]

    # dp[i]はi番目までたどり着くのに必要な最小コスト

    # dp[0] = 0

    # dp[1] = |h[1] - h[0]|

    # dp[i] = min(dp[0]+h[i]-h[0], dp[1]+h[i]-h[1], ...)

    dp = [0] * N

    dp[0] = 0

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

    for i in range(2, N):

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

    print((dp[N-1]))



if __name__ == "__main__":

    a()
def main():

    N, K = [int(i) for i in input().split()]

    A = [int(i) for i in input().split()]

    dp = [-1]*N

    dp[0] = 0

    for i in range(1,N):

        res = -1

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

            if i - j < 0:

                break

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

            if res == -1:

                res = new

                continue

            res = min(res, new)

        dp[i] = res

    print((dp[N-1]))

main()
def a():
    N, K = [int(n) for n in input().split()]
    h = [int(n) for n in input().split()]
    # dp[i]はi番目までたどり着くのに必要な最小コスト
    # dp[0] = 0
    # dp[1] = |h[1] - h[0]|
    # dp[i] = min(dp[0]+h[i]-h[0], dp[1]+h[i]-h[1], ...)
    dp = [0] * N
    dp[0] = 0
    dp[1] = abs(h[1] - h[0])
    for i in range(2, N):
        dp[i] = min([dp[k] + abs(h[k] - h[i]) for k in range(max(i - K, 0), i)])
    print((dp[N - 1]))


if __name__ == "__main__":
    a()

def main():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    dp = [-1] * N
    dp[0] = 0
    for i in range(1, N):
        res = -1
        for j in range(1, K + 1):
            if i - j < 0:
                break
            new = dp[i - j] + abs(A[i] - A[i - j])
            if res == -1:
                res = new
                continue
            res = min(res, new)
        dp[i] = res
    print((dp[N - 1]))


main()

-def a(): -    N, K = [int(n) for n in input().split()] -    h = [int(n) for n in input().split()] -    # dp[i]はi番目までたどり着くのに必要な最小コスト -    # dp[0] = 0 -    # dp[1] = |h[1] - h[0]| -    # dp[i] = min(dp[0]+h[i]-h[0], dp[1]+h[i]-h[1], ...) -    dp = [0] * N +def main(): +    N, K = [int(i) for i in input().split()] +    A = [int(i) for i in input().split()] +    dp = [-1] * N -    dp[1] = abs(h[1] - h[0]) -    for i in range(2, N): -        dp[i] = min([dp[k] + abs(h[k] - h[i]) for k in range(max(i - K, 0), i)]) +    for i in range(1, N): +        res = -1 +        for j in range(1, K + 1): +            if i - j < 0: +                break +            new = dp[i - j] + abs(A[i] - A[i - j]) +            if res == -1: +                res = new +                continue +            res = min(res, new) +        dp[i] = res -if __name__ == "__main__": -    a() +main()