"use strict";

function main(input){
  const args = input.trim().split('\n').map(_=>_.split(' '));
  const n = parseInt(args[0][0],10);
  const k = parseInt(args[0][1],10);
  const h = args[1].map(_=>parseInt(_,10));
  
  const dp = Array(n).fill(0); // i => i-stepでの最小コスト
  for(let i=1; i<n; i++){
    let minCost = dp[i-1] + Math.abs(h[i] - h[i-1]);
    for(let j=2; j<=k; j++){
      if(i-j < 0) break;
      const cost = dp[i-j] + Math.abs(h[i] - h[i-j]);
      minCost = Math.min(minCost, cost);
    }
    dp[i] = minCost;
  }
  
  console.log(dp[n-1]);
}

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