from sys import stdin, stdout

rl = lambda: stdin.readline()

rll = lambda: stdin.readline().split()

rli = lambda: list(map(int, stdin.readline().split()))



INF, NINF = float('inf'), float('-inf')



def main():

	n, k = rli()

	H = list(rli())

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

	dp[0] = 0

	for i in range(1, n):

		for j in range(max(0, i-k), i):

			cost = abs(H[j] - H[i])

			dp[i] = min(dp[i], dp[j] + cost)

	print((dp[-1]))

	stdout.close()



if __name__ == "__main__":

	main()
from sys import stdin, stdout, setrecursionlimit

from collections import deque, defaultdict, Counter

from heapq import heappush, heappop

import math



rl = lambda: stdin.readline()

rll = lambda: stdin.readline().split()

rli = lambda: list(map(int, stdin.readline().split()))

rlf = lambda: list(map(float, stdin.readline().split()))



INF, NINF = float('inf'), float('-inf')



def main():

	n, k = rli()

	A = list(rli())

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

	dp[0] = 0

	for i in range(1, n):

		for j in range(max(0, i-k), i):

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

	print((dp[-1]))

	stdout.close()



if __name__ == "__main__":

	main()
from sys import stdin, stdout

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: list(map(int, stdin.readline().split()))
INF, NINF = float("inf"), float("-inf")


def main():
    n, k = rli()
    H = list(rli())
    dp = [INF for _ in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for j in range(max(0, i - k), i):
            cost = abs(H[j] - H[i])
            dp[i] = min(dp[i], dp[j] + cost)
    print((dp[-1]))
    stdout.close()


if __name__ == "__main__":
    main()

from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
import math

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: list(map(int, stdin.readline().split()))
rlf = lambda: list(map(float, stdin.readline().split()))
INF, NINF = float("inf"), float("-inf")


def main():
    n, k = rli()
    A = list(rli())
    dp = [INF for _ in range(n)]
    dp[0] = 0
    for i in range(1, n):
        for j in range(max(0, i - k), i):
            dp[i] = min(dp[i], dp[j] + abs(A[j] - A[i]))
    print((dp[-1]))
    stdout.close()


if __name__ == "__main__":
    main()

-from sys import stdin, stdout +from sys import stdin, stdout, setrecursionlimit +from collections import deque, defaultdict, Counter +from heapq import heappush, heappop +import math +rlf = lambda: list(map(float, stdin.readline().split())) -    H = list(rli()) +    A = list(rli()) -            cost = abs(H[j] - H[i]) -            dp[i] = min(dp[i], dp[j] + cost) +            dp[i] = min(dp[i], dp[j] + abs(A[j] - A[i]))