def resolve():

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

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

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

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

	for i in range(2, n):

		s = []

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

			if i-j >= 0:

				s.append(dp[i-j] + abs(h[i-j] - h[i]))

		dp[i] = min(s)

	print((dp[-1]))

resolve()
def resolve():

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

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

			if i + j < n:

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

	print((dp[-1]))

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


resolve()

def resolve():
    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):
        for j in range(1, k + 1):
            if i + j < n:
                dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
    print((dp[-1]))


resolve()

-    dp[0], dp[1] = 0, abs(h[0] - h[1]) -    for i in range(2, n): -        s = [] +    dp[0] = 0 +    for i in range(n): -            if i - j >= 0: -                s.append(dp[i - j] + abs(h[i - j] - h[i])) -        dp[i] = min(s) +            if i + j < n: +                dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))