#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl "\n"
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef unordered_map<int, int> umap;
void inputarr(int arr[], int n) {REP(i, 0, n) {cin >> arr[i];}}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin>>n>>k;
int arr[n];
inputarr(arr,n);
vector<int>dp(n,0);
dp[0]=0;
dp[1]=abs(arr[1]-arr[0]);
for(int i=2;i<n;i++){
dp[i]=INT_MAX;
for(int j=1;j<=k;j++){
if(i-k>=0){
dp[i] = min(dp[i-k] + abs(arr[i]-arr[i-k]),dp[i]);
}
}
// int c1 = abs(arr[i-1]-arr[i])+dp[i-1];
// int c2 = abs(arr[i-2]-arr[i])+dp[i-2];
}
cout<<dp[n-1];
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgUkVQKGksYSxiKSBmb3IoaW50IGk9YTtpPGI7aSsrKQojZGVmaW5lIGYgZmlyc3QKI2RlZmluZSBzIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIGVuZGwgIlxuIgojZGVmaW5lIG1vZCAxMDAwMDAwMDA3CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZpOwp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+IHBpOwp0eXBlZGVmIHVub3JkZXJlZF9tYXA8aW50LCBpbnQ+IHVtYXA7CnZvaWQgaW5wdXRhcnIoaW50IGFycltdLCBpbnQgbikge1JFUChpLCAwLCBuKSB7Y2luID4+IGFycltpXTt9fQppbnQgbWFpbigpIHsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CgljaW4udGllKDApOwoJaW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