'use strict'
function main(input) {
  let tmp = input.split('\n');
  let k = tmp[0].split(' ')[1];
  let stones = tmp[1].split(' ').map(element => parseInt(element, 10));

  let dp = Array(stones.length).fill(Infinity);
  dp[0] = 0;
  for (let i = 0; i < stones.length; i++) {
    for(let j = i + 1; j <= i + k; j++) {
      if (j < stones.length) {
      	dp[j] = Math.min(dp[j, dp[i] + Math.abs(stones[i] - stones[j]))
       }
    }
  }
   console.log(dp[stones.length - 1])
}
      

main(require("fs").readFileSync("/dev/stdin", "utf8"));