#include <bits/stdc++.h>

using namespace std;
int v[100010];
int memo[100010];
int c;
int salto;
int ans=1000000000;
int f(int i)
{

    if(memo[i]!=-1)
        return memo[i];

    if(i == 0)
        return memo[0] = 0;

    if(i == 1)
        return memo[1] = abs(v[0] - v[1]);


    for(int k=1; k<=salto; k++)
    {
        ans=min(ans, abs(v[i] - v[i-k]) + f(i-k));
        return memo[i] = min(ans, abs(v[i] - v[i-k]) + f(i-k));
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    memset(memo, -1, sizeof(memo));
    cin>>c>>salto;

    for(int i = 0; i<c; i++)
    {
        cin>>v[i];
    }
    f(c-1);
    cout<<ans<<endl;

    return 0;
}
