// Original C++ solution (slower):
#pragma GCC optimize("O3")



#define _GLIBCXX_DEBUG

#include <iostream>

#include <vector>

#include <string>

#include <string_view>

#include <algorithm>

#include <cmath>

#include <iomanip>

#include <cassert>

#include <list>

#include <map>

#include <set>

#include <queue>

#include <deque>

#include <stack>

using namespace std;



/*----------macros----------*/



//output================

#define endl '\n'

#define fcout(a) cout << fixed << setprecision((int)(a))



//typedef===============

typedef long long       ll;

typedef long double     ld;

typedef pair<int, int>  Pii;

typedef pair<ll, ll>    Pll;



//const number==========

constexpr ll INF9   = (ll)1e9 + 1;

constexpr ll INF18  = (ll)1e18 + 1;

constexpr ll MOD    = 1000000007;

constexpr ll MOD2   = 998244353;

constexpr ld PI     = 3.141592653589793L;



//member================

#define pb push_back

#define eb emplace_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second



//others================

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

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

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

#define reps(i, n)      for(int i = 1; (i) <= (n); ++(i))

#define rrep(i, n)      for(int i = n - 1; (i) >= 0 ; --(i))

#define CtoI(c)         int((c)) - int('0')

#define ItoC(c)         char((c) + '0')



//functions=============

template<typename T1, typename T2>

inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }



template<typename T1, typename T2>

inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }



//vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};

//vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };



/*--------additional--------*/



int dp[100005];



/*----------main------------*/



int main() {

/*----------fastio----------*/

    std::cin.tie(nullptr);

    std::cout.tie(nullptr);

    std::ios::sync_with_stdio(false);

/*----------solve-----------*/



    int n, k; cin >> n >> k;

    vector<int> h(n);

    rep(i, n) cin >> h[i];

    for(auto &x : dp) x = INF9;



    dp[0] = 0;

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

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

            if(i - j >= 0)

                chmin(dp[i],

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

        }

    }



    cout << dp[n - 1] << endl;



return 0;

}

// Optimized solution to the same problem (faster):
#pragma GCC optimize("O3")



#define _GLIBCXX_DEBUG

#include <iostream>

#include <vector>

#include <string>

#include <string_view>

#include <algorithm>

#include <cmath>

#include <iomanip>

#include <cassert>

#include <list>

#include <map>

#include <set>

#include <queue>

#include <deque>

#include <stack>

using namespace std;



/*----------macros----------*/



//output================

#define endl '\n'

#define fcout(a) cout << fixed << setprecision((int)(a))



//typedef===============

typedef long long       ll;

typedef long double     ld;

typedef pair<int, int>  Pii;

typedef pair<ll, ll>    Pll;



//const number==========

constexpr ll INF9   = (ll)1e9 + 1;

constexpr ll INF18  = (ll)1e18 + 1;

constexpr ll MOD    = 1000000007;

constexpr ll MOD2   = 998244353;

constexpr ld PI     = 3.141592653589793L;



//member================

#define pb push_back

#define eb emplace_back

#define pf push_front

#define mp make_pair

#define fi first

#define se second



//others================

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

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

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

#define reps(i, n)      for(int i = 1; (i) <= (n); ++(i))

#define rrep(i, n)      for(int i = n - 1; (i) >= 0 ; --(i))

#define CtoI(c)         int((c)) - int('0')

#define ItoC(c)         char((c) + '0')



//functions=============

template<typename T1, typename T2>

inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }



template<typename T1, typename T2>

inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }



//vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};

//vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };



/*--------additional--------*/







/*----------main------------*/



int main() {

/*----------fastio----------*/

    std::cin.tie(nullptr);

    std::cout.tie(nullptr);

    std::ios::sync_with_stdio(false);

/*----------solve-----------*/



    int n, k; cin >> n >> k;

    vector<int> h(n);

    rep(i, n) cin >> h[i];

    int dp[n + 10];

    for(auto &x : dp) x = INF9;



    dp[0] = 0;

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

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

            if(i - j >= 0)

                chmin(dp[i],

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

        }

    }



    cout << dp[n - 1] << endl;



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;





}