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

#pragma GCC optimize "03"

using namespace std;



#define int long long int

#define pb push_back

#define pii pair<int,int>

#define fi first

#define se second

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

#define dbg(x) { cerr<<"> "<<#x<<": "<<x<< endl; }

#define dbg2(x,y) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }

#define dbg3(x,y,z) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }

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

#ifndef LOCAL

#define endl '\n'

#endif

const int inf = INT_MAX;

const double eps = 0.0000001;

const double PI = acos(-1.0);

const int MOD = 1e9+7;

const int N  = 1e5+5;



int a[N], dp[N], n, k;



int solve (int i) {

	if (i > n) return inf;

	if (i == n) return 0;

	if (i == n-1) return abs (a[n] - a[n-1]);

	if (dp[i] >= 0) return dp[i];

	int ans = inf;

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

		ans = min(ans, solve(j)+abs(a[j]-a[i]));

	}

	dp[i] = ans;

	return ans;

}



signed main(){

    #ifdef LOCAL

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

        //freopen("output.txt", "w", stdout);

    #endif  

    IOS;



    cin >> n >> k;

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

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

    	cin >> a[i];

    cout << solve(1);

    return 0;

}

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

#pragma GCC optimize "03"

using namespace std;



#define int long long int

#define pb push_back

#define pii pair<int,int>

#define fi first

#define se second

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

#define dbg(x) { cerr<<"> "<<#x<<": "<<x<< endl; }

#define dbg2(x,y) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<endl; }

#define dbg3(x,y,z) { cerr<<"> "<<#x<<": "<<x<<" , "<<#y<<": "<<y<<" , "<<#z<<": "<<z<<endl; }

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

#ifndef LOCAL

#define endl '\n'

#endif

const int inf = INT_MAX;

const double eps = 0.0000001;

const double PI = acos(-1.0);

const int MOD = 1e9+7;

const int N  = 1e5+5;



int a[N], dp[N];



signed main(){

    #ifdef LOCAL

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

        //freopen("output.txt", "w", stdout);

    #endif  

    IOS;



    int n, k;

    cin >> n >> k;

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

    	cin >> a[i];

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

    	dp[i] = inf;

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

    		if (i - j >= 1) {

    			dp[i] = min (dp[i], dp[i-j]+abs(a[i]-a[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;





}