#include "bits/stdc++.h"
using namespace std;
#define pb          push_back
#define mp          make_pair
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define rep(i,a,b)  for(int i=a;i<b;i++)
#define rev(i,a,b)  for(int i=a;i>=b;i--)
#define ll          long long
#define bg(i)       i.begin()
#define fset(it,s)   for(auto it = s.begin(); it != s.end(); it++)
#define track(x)                cerr<<#x<<": "<<x<<endl
#define track2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define track3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define track4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define ed(i)       i.end()
#define mem(a)	    memset(a, 0, sizeof(a))
#define MOD         100000000
ll mult(ll a,ll b, ll p=MOD){return ((a%p)*(b%p))%p;}
ll add(ll a, ll b, ll p=MOD){return (a%p + b%p)%p;}
ll fpow(ll n, ll k, ll p = MOD) {ll r = 1; for (; k; k >>= 1LL) {if (k & 1LL) r = mult(r,n,p); n = mult(n,n,p);} return r;}
ll inv(ll a, ll p = MOD) {return fpow(a, p - 2, p);}
void self_max(int &a, int &b) {a = max(a, b); }
void self_min(int &a, int &b) {a = min(a, b); }
#define hell        1000000007
bool isPrime(long int n) 
{
    // Corner cases 
    if (n <= 1) 
        return false; 
    if (n <= 3) 
        return true; 
  
    // This is checked so that we can skip 
    // middle five numbers in below loop 
    if (n % 2 == 0 || n % 3 == 0) 
        return false; 
  
    for (int i = 5; i * i <= n; i = i + 6) 
        if (n % i == 0 || n % (i + 2) == 0) 
            return false; 
  
    return true; 
}

long long power1(long long N, long long expo)
{
    long long ans = 1;
    
    while(expo)
    {
        if(expo % 2) {
            ans = ans * N % MOD;
            ans = ans % 10;
        }
        
        N = N * N % MOD;
        N = N % 10;
        expo /= 2;
    }
    
    return ans;
}

long long power(long long N, long long expo)
{
    long long ans = 1;
    
    while(expo)
    {
        if(expo % 2) {
            ans = ans * N % MOD;
        }
        
        N = N * N % MOD;
        expo /= 2;
    }
    
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) { 
    return (a.second < b.second); 
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ll dp[200005], arr[200005];
void solve(){
	int n, k;
	cin >> n >> k;
	rep(i, 0, n)	cin >> arr[i];
	// mem(dp);
	dp[0] = 0, dp[1] = abs(arr[0] - arr[1]);
	for(int i = 2; i < n; i++) {
		dp[i] = 1e9;
		for(int j = 1; j <= k; j++) {
			if(i - j >= 0)	dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
		}
	}

	cout << dp[n - 1] << endl;

}	
int main() {
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	// cin>>t;
	while(t--){
		solve();
	}
}



Measured memory_bytes: 1792