def main():

    import sys

    input = sys.stdin.buffer.readline

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

    h = [0] + [int(i) for i in input().split()]

    memo = [float('inf')] * (N+1)



    memo[1] = 0

    for j in range(2, N+1):

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

            if i < 0:

                continue

            memo[j] = min(memo[j], memo[i] + abs(h[j] - h[i]))



    print((memo[N]))





if __name__ == '__main__':

    main()

INF = 10**9 + 7





def main():

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

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

    dp = [INF]*N

    dp[0] = 0

    for i in range(N):

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

            if i + k >= N:

                break

            dp[i+k] = min(dp[i+k], dp[i] + abs(H[i+k] - H[i]))

    print((dp[-1]))





if __name__ == '__main__':

    main()

def main():
    import sys

    input = sys.stdin.buffer.readline
    N, K = (int(i) for i in input().split())
    h = [0] + [int(i) for i in input().split()]
    memo = [float("inf")] * (N + 1)
    memo[1] = 0
    for j in range(2, N + 1):
        for i in range(j - K, j):
            if i < 0:
                continue
            memo[j] = min(memo[j], memo[i] + abs(h[j] - h[i]))
    print((memo[N]))


if __name__ == "__main__":
    main()

INF = 10**9 + 7


def main():
    N, K = (int(i) for i in input().split())
    H = [int(i) for i in input().split()]
    dp = [INF] * N
    dp[0] = 0
    for i in range(N):
        for k in range(1, K + 1):
            if i + k >= N:
                break
            dp[i + k] = min(dp[i + k], dp[i] + abs(H[i + k] - H[i]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

+INF = 10**9 + 7 + + -    import sys - -    input = sys.stdin.buffer.readline -    h = [0] + [int(i) for i in input().split()] -    memo = [float("inf")] * (N + 1) -    memo[1] = 0 -    for j in range(2, N + 1): -        for i in range(j - K, j): -            if i < 0: -                continue -            memo[j] = min(memo[j], memo[i] + abs(h[j] - h[i])) -    print((memo[N])) +    H = [int(i) for i in input().split()] +    dp = [INF] * N +    dp[0] = 0 +    for i in range(N): +        for k in range(1, K + 1): +            if i + k >= N: +                break +            dp[i + k] = min(dp[i + k], dp[i] + abs(H[i + k] - H[i])) +    print((dp[-1]))