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

void main() {
	int n, stride;
	readf("%s %s\n", &n, &stride);
	auto steps = readln.chomp.split(' ').map!(to!int).array;

	auto dp = new int[n];
	dp[1..$] = int.max;

	foreach (i;0..n - 1) {
		foreach (j;i + 1..i + 1 + stride) {
			if (j >= n) {
				break;
			}

			dp[j] = min(dp[j], dp[i] + abs(steps[i] - steps[j]));
		}
	}

	writeln(dp[n - 1]);
}
