// Original C++ solution (slower):
// SeeAlso: https://atcoder.jp/contests/dp/tasks/dp_b



#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;



#define MAX 100000

#define NIL -1

#define MOD 1000000007



typedef int _loop_int;

#define rep(i,n) for(int i = 0; i < n; i++)

#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)

#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)



#define debug(x) cout<<#x<<": "<<x<<endl

#define debig_vec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl

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



// 最大公約数

inline constexpr ll gcd(ll a,ll b){if(!a||!b)return 0;while(b){ll c=b;b=a%b;a=c;}return a;}

// 最小公倍数

inline constexpr ll lcm(ll a,ll b){if(!a||!b)return 0;return a*b/gcd(a,b);}



#define print2D(h, w, arr) rep(i, h) { rep(j, w) cout << arr[i][j] << " "; cout << endl; }

template<class T> void print(const T& x){cout << setprecision(12) << x << endl;}

template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }



int n, k;

int h[MAX];





int main (){ 



    cin >> n >> k;

    memset(h, 0, sizeof(h));

    rep(i, n) {

        cin >> h[i];

    }

    

    ll dp[n+5];

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

    dp[0] = 0;

    rep(i, n) {

        FOR(j,i+1,min(i+k+1, n)) {

            if(dp[j] >= 0) {

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

            } else {

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

            }

        }

    }



    print(dp[n-1]);

    return 0;

}   

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



#include <bits/stdc++.h>



using namespace std;



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;



#define MAX 100000

#define NIL -1

#define MOD 1000000007



typedef int _loop_int;

#define rep(i,n) for(int i = 0; i < n; i++)

#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)

#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)



#define debug(x) cout<<#x<<": "<<x<<endl

#define debig_vec(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<endl

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



// 最大公約数

inline constexpr ll gcd(ll a,ll b){if(!a||!b)return 0;while(b){ll c=b;b=a%b;a=c;}return a;}

// 最小公倍数

inline constexpr ll lcm(ll a,ll b){if(!a||!b)return 0;return a*b/gcd(a,b);}



#define print2D(h, w, arr) rep(i, h) { rep(j, w) cout << arr[i][j] << " "; cout << endl; }

template<class T> void print(const T& x){cout << setprecision(12) << x << endl;}

template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }

template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}



int main() {

    ll n,k;

    cin >> n >> k;

    ll h[n];

    rep(i, n) {

        cin >> h[i];

    }



    ll dp[n+1];

    dp[0] = 0;

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

        dp[i] = dp[i-1] + abs(h[i]-h[i-1]);

        ll leng = min(k, i)+1;

        FOR(j, 1, leng) {

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

        }

    } 



    print(dp[n-1]);

    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;





}