'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf8');

// Your code here!
const max = (a)=>{
    let i = a.length;
    let max=-Infinity;
    while (i--) {
        if (a[i] > max)max = a[i];
    }
    return max;
}
const min = (a)=>{
    let i = a.length;
    let max=Infinity;
    while (i--) {
        if (a[i] < max)max = a[i];
    }
    return max;
}
const Main = (p)=>{
	p = p.split("\n");//2 or more
	//let N = parseInt(p[0]);//N
	let N = parseInt(p[0].split(" ")[0]);//N M
	let K = parseInt(p[0].split(" ")[1]);//N M
	let h = (p[1].split(" ").map(Number));//a1 a2 a3...

	let c = new Array(N).fill(Infinity);
    c[0]=0;
    c[1]=Math.abs(h[0]-h[1]);
    for(let i=2;i<N;i++){
        let t=[];
        for(let j=1;j<=K&&i-j>=0;j++){
            //console.log(i,j)
            t[j-1]=c[i-j]+Math.abs(h[i]-h[i-j]);
        }
        //console.log(t);
        c[i]=min(t);
    }
	//console.log(c);
	console.log(c[N-1]);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());
