import std.stdio;
import std.string;
import std.conv;
import std.array;
import std.math;

void main()
{
    auto buf = readln.split;
    int N = buf[0].to!int;
    int K = buf[1].to!int;
    int[] h = readln.split.to!(int[]);

    int[] cost = new int[N];
    cost[0] = 0;
    foreach (i; 1..N){
        int min_c = cost[i - 1] + abs(h[i] - h[i - 1]);
        foreach (j; 2..(K+1)){
            if (i - j < 0) break;

            int c = cost[i - j] + abs(h[i] - h[i - j]);
            if (c < min_c) min_c = c;
        }

        cost[i] = min_c;
    }

    writeln(cost[$ - 1]);
}

