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

using namespace std;

typedef long long ll;

#define MAXN 100100

const ll inf = (ll)1e10;

ll dp[MAXN];

int n, h[MAXN],k;

ll solve( int pos ) {

	if ( pos == n ) return 0;

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

	ll ret = inf;

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

		if ( pos + i <= n ) {

			ret = min(ret,solve(pos+i)+abs(h[pos]-h[pos+i]));

		}

	}

	return dp[pos] = ret;

}

int main()

{

	#ifdef __APPLE__

	//freopen("in.txt","r",stdin);

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

	#endif

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

	cin >> n >> k;

	for ( int i = 1; i <= n; i++ ) cin >> h[i];

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

	cout << solve(1) << endl;

	return 0;

}




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

    Author : Simanta Deb Turja

*/

#include <bits/stdc++.h>

using namespace std;



#define Professor



using ll = long long;

using i64 = unsigned long long;



template <typename T> inline T Min (T a, T b, T c) { return min(a, min(b, c)); }

template <typename T> inline T Min (T a, T b, T c, T d) { return min(a, min(b, min(c, d))); }

template <typename T> inline T Max (T a, T b, T c) { return max(a, max(b, c)); }

template <typename T> inline T Max (T a, T b, T c, T d) { return max(a, max(b, max(c, d))); }



template <typename T> inline T Ceil(T a, T b) { return ((a % b == 0) ? (a / b) : (a / b + 1)); } 

template <typename T> inline T Floor(T a, T b) { return a / b; }



template <typename T> inline T Power (T a, T p) { T res = 1, x = a; while ( p ) { if ( p & 1 ) res = ( res * x ); x = ( x * x ); p >>= 1;}return res; }

template <typename T> inline T BigMod (T a,T p,T M) { a %= M;T ret = 1; while (p) { if (p & 1) ret = (ret * a) % M; a = (a * a) % M; p = p >> 1; } return ret; }

template <typename T> inline T InverseMod (T a,T M) { return BigMod (a, M - 2, M); }



template <typename T> inline T gcd ( T a, T b ) { a = abs(a); b = abs(b); while ( b ) { a = a % b; swap ( a, b ); } return a; }

template <typename T> inline T lcm(T x,T y) { return (((x)/gcd((x),(y)))*(y)); }





#define endl '\n'

#define eb emplace_back



const int N = (int)4e5 + 10;

const double EPS = 1e-7;

const double PI = acos(-1.0);

const ll LLINF = numeric_limits<ll>::max();

const int INF = numeric_limits<int>::max();

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



template <typename T> bool cmp (const pair <T, T> &a, const pair <T, T> &b) { return a.first < b.first; }





int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr); cout.tie(nullptr);

    auto Solve = [&]() {

        #ifdef __APPLE__

            cout << "Running\n";

        #endif

        int n, k; 

        cin >> n >> k;

        vector <ll> h(n + 1, 0LL), dp(n + 1, (ll)1e10); 

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

            cin >> h[i];

        }

        dp[1] = 0LL;

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

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

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

            }

        }

        cout << dp[n] << endl;

    };

    Solve();

    

    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;





}