var inputRaw = require('fs').readFileSync('/dev/stdin', 'utf8')
var rows = inputRaw.trim().split('\n');
var row1 = rows[0].split(' ').map(Number);
var N = row1[0];
var K = row1[1];
var h = rows[1].split(' ').map(Number);
var dp = Array(N).fill(0);

function solve() {
  for (var i = 1; i < N; i++) {
    if (i === 1) {
      dp[i] = Math.abs(h[i - 1] - h[i]);
    } else {
      var list = [];
      for (var j = i - 1; j >= Math.max(i - K, 0); j--) {
        list.push(dp[j] + Math.abs(h[j] - h[i]));
      }
      dp[i] = Math.min.apply(null, list);
    }
  }
  return dp[N - 1];
}

console.log(solve());
