// Original C++ solution (slower):
/*

  Vivek Rathi

  CSE 2nd Year

  MNNIT

*/

#include<bits/stdc++.h>

using namespace std;

#define M 1000000007

#define ll long long int

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define debug1(x) cout<<#x<<" "<<x<<endl;

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

#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;

#define present(c,x) ((c).find(x) != (c).end())

#define null NULL

#define mp make_pair

#define fi first

#define se second

#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);

#define inf 1e17

#include <ext/pb_ds/assoc_container.hpp> 

#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ll n,h[100001],k;

ll dp[100001];

ll solve(ll i)

{

	if(i==n-1)

	{

		return 0;

	}

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

	vector<ll> v;

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

	{

		v.pb(abs(h[j]-h[i])+solve(j));

	}

	sort(v.begin(),v.end());

	return dp[i]=v[0];

}

int main()

{

	boost

	cin>>n>>k;

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

	for(ll i=0;i<n;i++) cin>>h[i];

	cout<<solve(0)<<endl;

	return 0;

}

// Optimized solution to the same problem (faster):
/*

  Vivek Rathi

  CSE 2nd Year

  MNNIT

*/

#include<bits/stdc++.h>

using namespace std;

#define M 1000000007

#define ll long long int

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define debug1(x) cout<<#x<<" "<<x<<endl;

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

#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;

#define present(c,x) ((c).find(x) != (c).end())

#define null NULL

#define mp make_pair

#define fi first

#define se second

#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);

#define inf 1e17

#include <ext/pb_ds/assoc_container.hpp> 

#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

int main()

{

	boost

	ll n,k;

	cin>>n>>k;

	ll h[n];

	for(ll i=0;i<n;i++) cin>>h[i];

	ll dp[n];

	for(ll i=0;i<n;i++) dp[i]=inf;

	dp[n-1]=0;

	for(ll i=n-2;i>=0;i--)

	{

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

		{

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

		}

	}

	cout<<dp[0]<<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;





}