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



#define fst first

#define snd second

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

#define pb push_back

#define ALL(s) s.begin(),s.end()

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

#define SZ(s) int(s.size())

#define DBG(x) cerr <<#x << " = " << (x) << "\n"



#define SQR(x) ((x) * (x))



#define PI 3.1415926535897932385

#define INF 1000111222

#define eps 1e-7

#define maxN 11



using namespace std;



typedef long long ll;

typedef vector<ll> vll;

typedef pair<ll,ll> pll;

typedef pair<int,int> pii;

typedef vector<string> vstr;

typedef vector<vll> vvll;

typedef vector<bool> vbool;

typedef vector<pll> vpll;

typedef vector<vpll> vvpll;

typedef vector<vbool> vvbool;



vll ns,dp;

ll n,k;



ll solve(ll pos){

	ll & d = dp[pos];

	if(d != -1) return d;

	ll v=INF;

	fore(i,pos+1,min(pos+k+1,n)) v = min(v,abs(ns[i]-ns[pos])+solve(i));

	if(pos == (n-1)) v = 0;

	return d = v;

}



int main(){FIN

	

	cin >> n >> k;

	ns.resize(n);

	dp.assign(100010,-1);

	fore(i,0,n)cin >> ns[i];

	

	solve(0);

	

	cout << dp[0] << "\n";



	return 0;

}


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

 

using namespace std;

 

typedef long long ll;

typedef vector<ll> vi;

typedef long double td;

typedef vector<td> vd;

typedef pair<ll,ll> ii;

typedef vector<ii> vii; typedef vector<bool> vb;

#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

#define forr(i, a, b) for(ll i = ll(a); i < (ll)(b); i++)

#define forn(i, n) forr(i, 0, n)

#define pb push_back

#define mp make_pair

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

#define DBG(x) cerr << #x << " = " << (x) << endl

#define show(v,n) cerr << #v << " = "; forn(i,n) cerr << v[i] << " "; cerr << endl;

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

#define RAYA cerr << "===============================" << endl

int MAXN = 500005;





ll solve(vector <int> &v, int n, int k)

{

	ll MAXN = (ll)(1e18);

	vector <ll> dp(n,MAXN);

	dp[0]=0;

	forr(i,1,n)

	{

		forr(j,1,k+1)

		{

			if(i-j < 0) break;

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

		}

	}

	return dp[n-1];

}

 

int main()

{ 	

	FIN;

	#ifdef input

		freopen("test1.txt", "r", stdin);

	#endif



	int n, k;

	cin >> n >> k;

	vector <int> v(n);

	forn(i,n) cin >> v[i];

	cout << solve(v,n,k) << 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;





}