n, k = list(map(int,input().split()))

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



dp = [float("inf")] * n

dp[0] = 0



for i in range(n-1):

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

        if i+j <= n-1:

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

print((dp[n-1]))
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()

n, k = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [float("inf")] * n
dp[0] = 0
for i in range(n - 1):
    for j in range(1, k + 1):
        if i + j <= n - 1:
            dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j])
print((dp[n - 1]))

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()

-n, k = list(map(int, input().split())) -h = list(map(int, input().split())) -dp = [float("inf")] * n -dp[0] = 0 -for i in range(n - 1): -    for j in range(1, k + 1): -        if i + j <= n - 1: -            dp[i + j] = min(dp[i] + abs(h[i] - h[i + j]), dp[i + j]) -print((dp[n - 1])) +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()