function main(input){
  var k = parseInt(input[0].split(' ')[1]);
  var line1 = input[1].split(' ');
  var arr = Array(line1.length);
  arr[0] = 0;
  for (var i=1; i<line1.length; i++){
    var min = null;
    for (var j=0; j<k; j++){
      if (i-j-1 >= 0) {
        var newMin = Math.abs(parseInt(line1[i-j-1]) - parseInt(line1[i])) + arr[i-j-1];
        if (min == null || newMin < min) min = newMin;
      }
    }
    arr[i] = min;
  }

  console.log(arr[line1.length-1]);
}


var input = '';

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
  input += chunk;
});
process.stdin.on('end', function() {
  input = input.split('\n');
  main(input);
});
