#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,k;
	cin>>n>>k;
	vector<int> h(n);
	for(int i = 0; i < n; i++) cin>>h[i];
	vector<long long> dp(n,INT_MAX);
	dp[0] = 0;
	for(int i = 1; i < n; i++){
		for(int j = 1; j <= k; j++){
			if(j <= i) dp[i] = min(dp[i],dp[i-j]+abs(h[i-j]-h[i]));
		}
	}
	cout<<dp[n-1];
}