#include <bits/stdc++.h>
using namespace std;

int main() {
  int N,K; cin>>N>>K;
  int h[N+1];
  for(int i=1;i<=N;i++)cin>>h[i];
  int dp[N+1];
  dp[1]=0;

  for(int i=2;i<=N;i++){
    dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
    for(int j=2;j<=K;j++){
      if(i>j)dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]));
    }
  }
  cout<<dp[N]<<endl;
}