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



using namespace std;

using ll =long long;

using db = double;



template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



#define che(a,string) cout<<"//"<<string <<"=="<<(a)<<"//" <<endl;

#define SORT(a) sort((a).begin(),(a).end())

#define rSORT(a) reverse((a).begin(),(a).end())

#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)

#define rep(i, n)       For(i, 0, n)

#define leftunique(v) SORT(v);(v).erase(unique((v).begin(),(v).end()),(v).end())

//leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4 6 になる

bool IsInt(double a){int b = a/1;if(a==b){return true;}else {return false;}}

bool coY() {cout <<"Yes"<<endl;}

bool coN(){cout <<"No"<<endl;}

const int mod = 1e9 + 7;

const ll INF = 1LL << 60;



//ここから書き始める

int main()

{

	cout << setprecision(10);

	ll n, k;

	cin >> n >> k;

	vector<ll> h(110010);

	rep(i,n) cin >> h[i];



	vector<ll> dp(110010,INF);

	dp[0] = 0;

	rep(i,n){

		For(j,1,k+1){

			chmin(dp[i+j],dp[i]+abs(h[i] - h[i+j]));

		}

	}

	

	

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



}


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



using namespace std;

using ll =long long;

using db = double;



template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



#define che(a,string) cout<<"//"<<string <<"=="<<(a)<<"//" <<endl;

#define SORT(a) sort((a).begin(),(a).end())

#define rSORT(a) reverse((a).begin(),(a).end())

#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)

#define rep(i, n)       For(i, 0, n)

#define leftunique(v) SORT(v);(v).erase(unique((v).begin(),(v).end()),(v).end())

//leftuniqueは配列から同じ要素のものを取り除く 例 4 6 2 1 3 4 1 2 なら 1 2 3 4 6 になる

bool IsInt(double a){int b = a/1;if(a==b){return true;}else {return false;}}

bool coY() {cout <<"Yes"<<endl;}

bool coN(){cout <<"No"<<endl;}

const int mod = 1e9 + 7;

const ll INF = 1LL << 60;



//ここから書き始める

int main()

{

	cout << setprecision(10);

	ll n ,k ;

	cin >> n >> k;

	vector<ll> h(n);

	rep(i,n) cin >>h[i];



	vector<ll> dp(101000,INF);



	dp[0]=0;

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

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

			chmin(dp[i+j], dp[i] + abs( h[i] - h[i+j]));

		}

	}

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

}


// 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;





}