// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<bitset>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define ARDEB(i,X) {}

#define END {}

#endif

typedef long long int ll;

#define int long long

typedef pair<int,int> P;

struct edge{int to,cost;};

#define REP(i,n) for(int i=0;i<(n);i++)

const int INF=100000000000000000;



int N,A[100110],K;

int dp[100110];



int DP(int a){

	if(dp[a]!=-1)return dp[a];

	vector<int> v;

	if(a==N-1){

		return 0;

	}

	DEB(a)END

	for(int i=1;i<=K;i++){

		if(a<N-i){

			v.push_back(DP(a+i)+abs(A[a]-A[a+i]));

		}

	}

	return dp[a]=(*min_element(v.begin(),v.end()));

}



signed main(){

	cin>>N>>K;

	REP(i,N){

		cin>>A[i];

	}

	REP(i,100010){

		dp[i]=-1;

	}

	cout<<DP(0)<<endl;

	return 0;

}


// Optimized solution to the same problem (faster):
#include<iostream>

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<bitset>

#include<iomanip>

#include<list>

#include<deque>

using namespace std;

//#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define END {}

#endif

typedef long long ll;

#define int ll

#define uset unordered_set

#define umap unordered_map

typedef std::pair<int,int> P;

struct edge{int to,cost;};

const int INF=100000000000000000;

const int INF2=9223372036854775807;

const int MOD=1000000007;

#define REP(i,n) for(int i=0;i<(int)(n);i++)

#define min(X,Y) (((int)(X)<(int)(Y))?(X):(Y))

#define max(X,Y) (((int)(X)>(int)(Y))?(X):(Y))

#define MAX(X,Y) (*max_element(X,Y))

#define MIN(X,Y) (*min_element(X,Y))

#define NP(X,Y) next_permutation(X,Y)

#define setp(X,Y) cout<<fixed<<setprecision(Y)<<X;

int ceil2(int a,int b){if(a%b==0){return a/b;}else{return a/b+1;}}

int pow2(int a,int b){int r=1;for(int i=1;i<=b;i++){r*=a;}return r;}

int Log2(int a){int t=0;while(1){if(a==0||a==1){break;}a/=2;t++;}return t;}



int N,K;

int H[102010];

int dp[102000];



signed main(){

	cin>>N>>K;

	REP(i,N){

		cin>>H[i];

		dp[i]=INF;

	}

	dp[0]=0;

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

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

			dp[i+j]=min(dp[i+j],dp[i]+abs(H[i]-H[i+j]));

		}

	}

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

	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;





}