// author : anphung
#include<bits/stdc++.h>
using namespace std;
#define file "GAME"
#define int64 long long
#define f0(i,a,b) for(int (i) = (a); (i) <= (b); ++i)
#define inf 1e18
#define TIME (1.0*clock()/CLOCKS_PER_SEC)
int n,k;
vector<int> h;
int64 ans = 1e18;
void Try(int pos, int64 d){
if(pos==n){
ans = min(ans, d);
return;
}
for(int i=1; i<=k && pos+i<=n;++i){
Try(pos+i, d+abs(h[pos]-h[i+pos]));
}
}
void trau(){
Try(1,0);
cout<<ans;
}
void sol(){
vector<int64> dp(n+1, inf);
dp[1] = 0;
f0(i,1,n){
f0(j,i+1,min(n,i+k)){
dp[j] = min(dp[j], dp[i] + abs(h[i]-h[j]));
}
}
cout<<dp[n];
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
freopen(file".inp","r",stdin);
freopen(file".out","w",stdout);
cin>>n>>k;
h.resize(n+1);
f0(i,1,n)cin>>h[i];
sol();
//cerr<<"\ntime elapsed: "<<TIME <<"s.\n";
}
Ly8gYXV0aG9yIDogYW5waHVuZwojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGZpbGUgIkdBTUUiCiNkZWZpbmUgaW50NjQgbG9uZyBsb25nCiNkZWZpbmUgZjAoaSxhLGIpIGZvcihpbnQgKGkpID0gKGEpOyAoaSkgPD0gKGIpOyArK2kpCiNkZWZpbmUgaW5mIDFlMTgKI2RlZmluZSBUSU1FICgxLjAqY2xvY2soKS9DTE9DS1NfUEVSX1NFQykKCmludCBuLGs7CnZlY3RvcjxpbnQ+IGg7CmludDY0IGFucyA9IDFlMTg7Cgp2b2lkIFRyeShpbnQgcG9zLCBpbnQ2NCBkKXsKICAgIGlmKHBvcz09bil7CiAgICAgICAgYW5zID0gbWluKGFucywgZCk7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgZm9yKGludCBpPTE7IGk8PWsgJiYgcG9zK2k8PW47KytpKXsKICAgICAg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