'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}

function getArray(str) {
    return str.split(' ').map(x => Number(x));
}

function getInt(str) {
    return Number(str);
}

function getStrArray(str) {
    return str.split(' ').map(x => x);
}
// ---------------------------------------



function main() {
    const x = getArray(readline());
    const len = x[0];
    const k = x[1];
    const a = getArray(readline());
    const dp = new Array(len);
    dp.fill(Number.MAX_VALUE);
    dp[0] = 0;
    dp[1] = Math.abs(a[1] - a[0]);
    for (let i = 2; i < len; i++) {
        // for(let j = 1; j <= k; j++) {
        //     if (i - j >= 0) {
        //         dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(a[i] - a[i - j]));
        //     }
        // }

        for (let j = i - 1; j >= Math.max(1, i - k); j--){
            dp[i] = Math.min(dp[i], dp[j] + Math.abs(a[i] - a[j]));
        }
    }
    console.log(dp[len-1]);
}