#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
const int oo = 1000000000;
int n, k, v[N], dp[N];
int f(int pos) {
	if(dp[pos] != -1)
		return dp[pos];
	if (pos == (n - 1))
		return 0;
	if (pos > n)
		return oo;
	int answer = oo;
	for(int i = 1; i <= k; i++)
		answer = min(answer, f(pos + i) + abs(v[pos] - v[pos + i]));
	return dp[pos] = answer;
}
int main(){
	cin >> n >> k;
	memset(dp, -1, sizeof dp);
	for(int i = 0; i < n; i++)
		cin >> v[i];
	cout << f(0) << "\n";
    return 0;
}
