'use strict';
console.log(function(args){
  const tmp = args.trim().split('\n').map( r=>r.split(' ').map(v=>v|0) );
  const N = tmp[0][0], K = tmp[0][1];
  const h = tmp[1];
  const dp = Array(N).fill(Infinity);
  dp[0] = 0;
  for ( let i = 0; i < N-1; i++ ) {
    for ( let j = 1; j <= K; j++ ) {
      const cost = Math.abs( h[i] - h[i+j] ) + dp[i];
      if ( dp[i+j] > cost ) dp[i+j] = cost
      if ( i + j === N ) break;
    }
  }
  return dp[N-1].toString();
}(require('fs').readFileSync('/dev/stdin', 'utf8')));
