// Original C++ solution (slower):
//Robs Code

/***********HEADER***************/

#include <iostream>

#include <bitset>

#include <deque>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <algorithm>

#include <list>

#include <vector>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <set>

#include <queue>

#include <map>

/***********MACROS***************/

#define int long long int

#define fri(l,k) for (i=l;i<=k;i++)

#define frj(l,k) for (j=l;j>=k;j--)

#define fij(a,b,l,k) for(i=a;i<=b;i++) for(j=l;j<=k;j++)

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

#define allr(x) x.rbegin(),x.rend()

#define endl "\n"

#define pb push_back

#define _1 first

#define _2 second

#define mxsz 1e5+10

#define mod 1e9 + 7

#define pii pair<int,int>

using namespace std;

const int N = 1e5 + 1;

int n,m,i,j,l,k,x,y,a,b,cnt[3],sum[3];

/***********MAIN**************/

vector<int> dp,v;

int brute(int indx){

	if(indx >= (n)){

		return 1e9;

	}

	if(indx == (n-1))

		return 0;

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

		return dp[indx];

	}

	int ans = 1e9;

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

		ans = min(ans,brute(indx+i) + abs(v[indx] - v[indx+i]));

	return dp[indx] = ans;

}

signed main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin>>n>>k;

	v.assign(n,0);

	for(auto &x : v){

		cin>>x;

	}

	dp.assign((int)1e5+10,-1);

	cout<<brute(0);

    return 0;

}

//.........

// Optimized solution to the same problem (faster):
//Robs Code

/***********HEADER***************/

#include <iostream>

#include <bitset>

#include <deque>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <algorithm>

#include <list>

#include <vector>

#include <unordered_set>

#include <unordered_map>

#include <stack>

#include <set>

#include <queue>

#include <map>

/***********MACROS***************/

#define int long long int

#define fri(l,k) for (i=l;i<=k;i++)

#define frj(l,k) for (j=l;j>=k;j--)

#define fij(a,b,l,k) for(i=a;i<=b;i++) for(j=l;j<=k;j++)

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

#define allr(x) x.rbegin(),x.rend()

#define endl "\n"

#define pb push_back

#define _1 first

#define _2 second

#define pii pair<int,int>

using namespace std;

int n,m,i,j,l,k,x,y,a,b,cnt[3],sum[3];

/***********MAIN**************/

signed main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin>>n>>k;

	int dp[n+1],h[n+1];

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

		cin>>h[i];

	}

	dp[1] = 0;

	dp[2] = abs(h[2] - h[1]);

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

		dp[i] = 1e18;

		for(int j = 1;j <= k && (i-j) >= 1;j++){

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

		}

	}

	cout<<dp[n];

	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;





}