'use strict'
function main(input) {
  const tmp = input.split('\n');
  const NK = tmp[0].split(' ').map((elem) => parseInt(elem, 10));
  const N = NK[0];
  const K = NK[1];
  const h = tmp[1].split(' ').map((elem) => parseInt(elem, 10));

  let dp = new Array(N);
  dp.fill(Infinity);
  dp[0] = 0;
  for (let i = 1; i < N; i++) {
    for (let k = 1; k <= K; k++) {
      if (i >= k) {
        dp[i] = Math.min(dp[i], dp[i - k] + Math.abs(h[i] - h[i - k]));
      }
    }
  }

  console.log(dp[N - 1]);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));