#include <bits/stdc++.h>

using namespace std;

#define INF (1ll << 62)
#define pb push_back
#define ii pair<int,int>
#define OK cerr <<"OK"<< endl
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define endl '\n'

signed main () {
  
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;

  cin >> n >> k;

  vector<int> dp(n + 1);
  vector<int> arr(n + 1);

  for(int i = 1; i <= n; i++) {
  	cin >> arr[i];
  }

  for(int i = 2; i <= n; i++) {
  	dp[i] = INF;
  	for(int j = 1; j <= k; j++) {
  		if(i - j >= 1) {
  			dp[i] = min(dp[i], abs(arr[i] - arr[i - j]) + dp[i - j]);
  		} else {
  			break;
  		}
  	}
  }

  cout << dp[n] << endl;
}
