#include <bits/stdc++.h>
using namespace std;
/// Luu y code tham khao dung [0, n) thay vi [1, n]
void minimize(int &a, int b) { a = min(a, b); }
int main()
{
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &x : a) cin >> x;
vector<int> dp(n, 0);
for (int i = 1; i < n; ++i)
{
dp[i] = dp[i - 1] + abs(a[i] - a[i - 1]);
for (int j = i - 2; j >= max(0, i - k); --j)
minimize(dp[i], dp[j] + abs(a[i] - a[j]));
}
cout << dp.back();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Ci8vLyBMdXUgeSBjb2RlIHRoYW0ga2hhbyBkdW5nIFswLCBuKSB0aGF5IHZpIFsxLCBuXQp2b2lkIG1pbmltaXplKGludCAmYSwgaW50IGIpIHsgYSA9IG1pbihhLCBiKTsgfQppbnQgbWFpbigpCnsKICAgIGludCBuLCBrOwogICAgY2luID4+IG4gPj4gazsKCiAgICB2ZWN0b3I8aW50PiBhKG4pOwogICAgZm9yIChpbnQgJnggOiBhKSBjaW4gPj4geDsKCiAgICB2ZWN0b3I8aW50PiBkcChuLCAwKTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgbjsgKytpKQogICAgewogICAgICAgIGRwW2ldID0gZHBbaSAtIDFdICsgYWJzKGFbaV0gLSBhW2kgLSAxXSk7CiAgICAgICAgZm9yIChpbnQgaiA9IGkgLSAyOyBqID49IG1heCgwLCBpIC0gayk7IC0taikKICAgICAgICAgICAgbWluaW1pemUoZHBbaV0sIGRwW2pdICsgYWJzKGFbaV0gLSBhW2pdKSk7CiAgICB9CgogICAgY291dCA8PCBkcC5iYWNrKCk7CiAgICByZXR1cm4gMDsKfQ==