function Main(input) {
    var aryMin = function (a, b) {return Math.min(a, b);}
    
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});

    let N = parseInt(input[0][0], 10);
    let K = parseInt(input[0][1], 10);
    let v_h = input[1].map(e => parseInt(e, 10));

    let DP = [];

    for (let i = 0; i < K; i++){
        DP.push(Math.abs(v_h[0] - v_h[i]));
    }

    let x = [];
    for(let i = K; i < N; i++){
        for (let j = 0; j < K; j++){
            x[j] = DP[i - K + j] + Math.abs(v_h[i] - v_h[i - K + j]);
        }
        DP.push(x.reduce(aryMin));
    }

    console.log(DP[N - 1]);

}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
