# -*- coding: utf-8 -*-

"""

B - Frog 2

https://atcoder.jp/contests/dp/tasks/dp_b

AC

"""

import sys

from sys import stdin





def solve(ashiba, N, K):

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

    dp[0] = 0

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

        paths = [dp[j]+abs(ashiba[j]-ashiba[i]) for j in range(max(0, i-K), i)]

        dp[i] = min(paths)

    return dp[-1]





def main(args):

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

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

    ans = solve(ashiba, N, K)

    print(ans)





if __name__ == '__main__':

    main(sys.argv[1:])

# -*- coding: utf-8 -*-

"""





"""





def chmin(i, j):

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

    if a > b:

        dp[i+j] = b





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

h = list(input().split())

h = [int(i) for i in h]

dp = [10**10]*n

dp[0] = 0

for i in range(0, n):

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

        if i+j < n:

            chmin(i, j)

print((dp[n-1]))
# -*- coding: utf-8 -*-
"""
B - Frog 2
https://atcoder.jp/contests/dp/tasks/dp_b
AC
"""
import sys
from sys import stdin


def solve(ashiba, N, K):
    dp = [float("inf")] * N
    dp[0] = 0
    for i in range(1, len(ashiba)):
        paths = [dp[j] + abs(ashiba[j] - ashiba[i]) for j in range(max(0, i - K), i)]
        dp[i] = min(paths)
    return dp[-1]


def main(args):
    N, K = list(map(int, input().split()))
    ashiba = [int(i) for i in input().split()]
    ans = solve(ashiba, N, K)
    print(ans)


if __name__ == "__main__":
    main(sys.argv[1:])

# -*- coding: utf-8 -*-
"""
"""


def chmin(i, j):
    a, b = dp[i + j], dp[i] + abs(h[i] - h[i + j])
    if a > b:
        dp[i + j] = b


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

-B - Frog 2 -https://atcoder.jp/contests/dp/tasks/dp_b -AC -import sys -from sys import stdin -def solve(ashiba, N, K): -    dp = [float("inf")] * N -    dp[0] = 0 -    for i in range(1, len(ashiba)): -        paths = [dp[j] + abs(ashiba[j] - ashiba[i]) for j in range(max(0, i - K), i)] -        dp[i] = min(paths) -    return dp[-1] +def chmin(i, j): +    a, b = dp[i + j], dp[i] + abs(h[i] - h[i + j]) +    if a > b: +        dp[i + j] = b -def main(args): -    N, K = list(map(int, input().split())) -    ashiba = [int(i) for i in input().split()] -    ans = solve(ashiba, N, K) -    print(ans) - - -if __name__ == "__main__": -    main(sys.argv[1:]) +n, k = list(map(int, (input().split()))) +h = list(input().split()) +h = [int(i) for i in h] +dp = [10**10] * n +dp[0] = 0 +for i in range(0, n): +    for j in range(1, k + 1): +        if i + j < n: +            chmin(i, j) +print((dp[n - 1]))