#include <cstdio>
#include <algorithm>
#include <cstdint>
#include <cinttypes>
#include <cstring>

using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;

inline i32 diff(i32 x, i32 y) {
	return x > y ? x - y : y - x;
}

const i32 MaxN = 100001;

i32 dp[MaxN], A[MaxN];

signed main(signed argc, char **argv) {
	i32 n, k;
	scanf("%" SCNdFAST32 "%" SCNdFAST32, &n, &k);

	for (i32 i = 0; i < n; ++ i) {
		scanf("%" SCNdFAST32, A + i);
	}
	
	memset(dp, 0x3f, sizeof dp);
	
	dp[0] = 0;

	for (i32 i = 0; i < n; ++ i)  {
		for (i32 j = 1; j <= k; ++ j) {
			if (i - j < 0) {
				break;
			}
			dp[i] = std::min(dp[i], dp[i - j] + diff(A[i], A[i - j]));
		}
	}

	printf("%" SCNdFAST32, dp[n - 1]);
	return 0;
}