const inf = Number.MAX_SAFE_INTEGER;
function Main(input){
	input = input.split("\n");
	var mem = input[0].split(" ").map(Number);
	var k = mem[1];
	var arr = input[1].split(" ").map(Number);
	var dp = new Array(mem[0]).fill(0);
	var l = 0;
	for(var i=1;i<arr.length;i++){
		var min = inf;
		for(var j=i-1;j!=-1&&l<k;l++,j--){
			var n = dp[j]+Math.abs(arr[j]-arr[i]);
		//	console.log(n,i,j)
			if(n<min) min = n;
		}
		dp[i] = min;
		l = 0;
	}
	console.log(dp[dp.length-1]);
//	console.log(dp)
}Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());