#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false), cin.tie(0)
typedef long long ll;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
ll a[maxn];
ll dp[maxn];

int main()
{
    IO;
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    memset(dp, inf, sizeof(dp));
    dp[1] = 0;
    for (int i = 2; i <= n; ++i)
    {
        for (int j = 1; j <= k; ++j)
        {
            dp[i] = min(dp[i], dp[max(i - j, 1)] + abs(a[i] - a[max(i - j, 1)]));
        }
    }
    cout << dp[n] << endl;
}
