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

#define F first

#define S second

#define pb push_back

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

#define sz(x) (int)(x).size()

#define fill(x,v) memset(x, v, sizeof(x))

#define PI (ld)acos(-1)

#define endl '\n'

#define dbg(x) cout<<#x<<"="<<(x)<<endl;

#define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; 

#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 

#define IO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef pair<int,int> pi;

typedef pair<ll,ll> pl;

const ld eps=(ld)1e-9;

const ll inf=(ll)1e18;

const ll mod=(ll)1e9+7;

int n,k, h[100007],dp[100007];

int f(int i){

	if(i==n-1){

		return 0;

	}

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

		return dp[i];

	}

	int ans=INT_MAX;

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

	{

		ans=min(ans,abs(h[j]-h[i])+f(j));

	}

	return dp[i]=ans;

}





void solve(){

	cin>>n>>k;

	fill(dp,-1);

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

	{

		cin>>h[i];

	}

	cout<<f(0);

}



int main(){fio;

int test=1;

// cin>>test;

while(test--){

	solve();

	cout<<endl;

}

return 0;

}

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

#define F first

#define S second

#define pb push_back

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

#define sz(x) (int)(x).size()

#define fill(x,v) memset(x, v, sizeof(x))

#define PI (ld)acos(-1)

#define endl '\n'

#define dbg(x) cout<<#x<<"="<<(x)<<endl;

#define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; 

#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 

#define IO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

typedef pair<int,int> pi;

typedef pair<ll,ll> pl;

const ld eps=(ld)1e-9;

const ll inf=(ll)1e9;

const ll mod=(ll)1e9+7;





void solve(){

	int n,k;

	cin>>n>>k;

	int h[n];

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

	{

		cin>>h[i];

	}

	vector<int>dp(n,inf);

	dp[0]=0;

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

	{

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

		{

			if(j<n){

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

			}

		}

	}

	cout<<dp[n-1];

}



int main(){fio;

int test=1;

// cin>>test;

while(test--){

	solve();

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





}