// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize ("-ffloat-store")  

#pragma GCC optimize ("-fno-defer-pop")

#define all(a) a.begin(),a.end()

#define ll long long int

#define ld long double

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

#define ps push_back

#define fs first

#define sc second

#define N 300005

#define endl "\n"

#define mod 1000000007

ll n,k,ar[N],dp[N];

ll minval(ll i){

	if(i==n){

		return 0;

	}

	if(dp[i]!=-1){

		return dp[i];

	}



	ll j,mn=1e18;

	for(j=1;j<=k&&i+j<=n;j++){

		mn=min(mn,minval(i+j)+abs(ar[i+j]-ar[i]));

	}

	return dp[i]=mn;

}

int main(){



	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	

	ll i,j,l;

	cin>>n>>k;

	memset(dp,-1,sizeof(dp));

	for(i=1;i<=n;i++){

		cin>>ar[i];

	}

	

	l=minval(1);

	cout<<l;

	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision

#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns

#define all(a) a.begin(),a.end()

#define ll long long int

#define ld long double

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

#define ps push_back

#define fs first

#define sc second

#define takeline cin.ignore();

#define iactive cout.flush();

#define N 3000005

#define endl "\n"

#define mod 1000000007

//((1.0l)*BIG MULTIPLY MAGIC?)

// string to integer stoi()

// string to long long stoll()

// string.substr(position,length);

// integer to string to_string();

//-----------------------------------------------

ll n,k,ar[100005],dp[100005];

ll findmin(ll i){

	if(i==n){

		return 0;

	}

	if(dp[i]!=-1){

		return dp[i];

	}

	ll an=1e18,j;

	for(j=1;j<=k;j++){

		if(i+j<=n){

			an=min(an,findmin(i+j)+abs(ar[i]-ar[i+j]));

		}

	}

	return dp[i]=an;

}

int main(){



	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);



	ll i,j,l;

	cin>>n>>k;

	for(i=1;i<=n;i++){

		cin>>ar[i];

	}

	memset(dp,-1,sizeof(dp));

	ll an=findmin(1);

	cout<<an;

	return 0;

}

// Fastest known solution for this problem:
#include <iostream>

#include <array>

#include <vector>

#include <iomanip>

#include <string>

#include <cmath>

#include <array>

#include <algorithm>

#include <queue>

#include <map>

using namespace std;



typedef  pair<int,int> P;

typedef long long LL;



const LL  INF=1<<31;







int main(){

     int n,k;

     cin>>n>>k;

     int h[100000];

     for(int i=0;i<n;++i){

          cin>>h[i];

     }

     LL dp[100001];



      for(int i=0;i<n;++i){

           dp[i]=INF;

      }

            dp[0]=0;

            dp[1]=abs(h[0]-h[1]);







     for(int i=2;i<n;++i){

          dp[i]=dp[i-1]+abs(h[i-1]-h[i]);

          for(int j=2;j<=k;++j){



               if(j<=i)dp[i]=min(dp[i],dp[i-j]+abs(h[i-j]-h[i]));

          }

     }     



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





}