'use strict';

function Main(inputs) {
  // 1行目がinput[0], 2行目がinput[1], …に入る
  const input = inputs.split("\n");
  const n = Number(input[0].split(' ')[0]);
  const k = Number(input[0].split(' ')[1]);
  const h = input[1].split(' ').map(Number);
  const dp = Array(n).fill(1e10);
  dp[0] = 0;

  for (let i = 0; i < n; i++) {
    for (let diff = 1; diff <= k; diff++){
      if (i + diff >= n) continue;
      dp[i + diff] = Math.min(dp[i + diff], dp[i] + Math.abs(h[i] - h[i + diff]));
    }
  }
  console.log(dp[n - 1]);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
