#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mod 1000000007
#define null NULL
#define test cout<<"#"
#define maX 1000000000
 
vector<ll> ans(100001, LONG_LONG_MAX);
void solve(vector<ll> &v, ll n, ll k)
{
	ans[0] = 0;
	for(ll x = 1; x < n; x++)
	{	
		ans[x] = 1000000000000;
		for(ll y = 1; y <= k; y++)
		{
			if((x-y)>=0)
				ans[x] = min(ans[x], abs(v[x]-v[x-y]) + ans[x-y]);
		}
	}
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n, k;
	cin>>n>>k;
    vector<ll> v(n);
    for(ll x = 0; x < n; x++)
		cin>>v[x];
	solve(v, n, k);
	cout<<ans[n-1];
}