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

//          ,.                    //

//         (_|,.                  //

//        ,' /, )_______   _      //

//     __j o``-'        `.'-)'    //

//    (")                 \'      //

//     `-j                |       //

//        `-._(           /       //

//           |_\  |--^.  /        //

//          /_]'|_| /_)_/         //

//             /_]'  /_]'         //

//                                //

//           PIGMEISTER™          //

//                                //

////////////////////////////////////



#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define ull unsigned long long

#define mod (ll)1000000007

#define sidha(i, x, n) for(ll i=(ll)(x);i<(ll)(n);i++)

#define ulta(i, x, n) for(ll i=(ll)(x);i>=(ll)(n);i--)

#define mii map<ll, ll>

#define vi vector<ll>

#define pii pair<ll, ll>

#define pb push_back

#define mp make_pair

#define modInv(x) powah(x, mod-2)

#define test ll t;cin>>t;while(t--)

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

#define endl '\n'

//ll powah(ll, ll);



vi dp(100005, -1), h;



ll minCost(ll n, ll k) {

    if (dp[n] > -1) return dp[n];



    if (n == 1)

        dp[n] = 0;

    else {

        ll miin = LLONG_MAX, limit = min(k+1, n);

        sidha (i, 1, limit)

            if (miin > minCost(n-i, k) + abs(h[n] - h[n-i]))

                miin = minCost(n-i, k) + abs(h[n] - h[n-i]);

        dp[n] = miin;

    }

    return dp[n];

}



int main()

{

    jaldi;

    

    ll n, k, a;

    cin >> n >> k;

    h.pb(0);

    sidha (i, 0, n) {

        cin >> a;

        h.pb(a);

    }

    cout << minCost(n, k) << endl;

    

    return 0;

}



// ll powah(ll x, ll y) { 

//     ll res = 1;

//     x = x % mod;

//     while (y > 0) { 

//         if (y & 1) 

//             res = (res*x) % mod; 

//         y = y>>1;

//         x = (x*x) % mod; 

//     } 

//     return res; 

// }

// Optimized solution to the same problem (faster):
//   PIGMEISTER™   //



#include <bits/stdc++.h>

using namespace std;



#define ll long long

#define ld long double

#define sidha(i,x,n) for(ll i=(ll)(x);i<(ll)(n);++i)

#define ulta(i,x,n) for(ll i=(ll)(x);i>=(ll)(n);--i)

#define sab(a) a.begin(),a.end()

#define bas(a) a.rbegin(),a.rend()

#define test ll _;cin>>_;while(_--)

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

#define MOD (ll)1000000007

//#define MOD (ll)998244353

#define PI (ld)3.141592653589793238

#define MAX 100005



int main()

{

    jaldi;

    

    ll n, k;

    cin >> n >> k;

    ll h[n];

    sidha (i, 0, n) cin >> h[i];



    ll dp[n] = {0};

    sidha (i, 1, n) {

        dp[i] = 1e18;

        sidha (j, max(i-k, 0ll), i) {

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

        }

    }



    cout << dp[n-1] << '\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;





}