"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0].split(" ")[0]);
    const K = parseInt(arg[0].split(" ")[1]);
    
    const steps = arg[1].split(" ").map(n=>parseInt(n));
    
    const dp = [...Array(N)].fill(Infinity);
    
    dp[0] = 0;
    
    for(let i=1; i<N; i++) {
        let temp = Infinity;
        
        for(let j=1; j<=K; j++) {
            if(i-j >= 0) {
                dp[i] = Math.min(temp, dp[i-j] + Math.abs(steps[i] - steps[i-j]));
            }
        }
    }
    
    console.log(dp[N-1]);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));