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