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

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

#define rrep(i,a,b) for(int i=a;i>=b;i--)

#define fore(i,a) for(auto &i:a)

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

//#pragma GCC optimize ("-O3")

using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }

typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------

int N, K;

vector<int> h;



int dp[101010];



int Solve(int pos)

{

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



    if (pos == N - 1) return 0;

    if (pos >= N) return inf;



    int res = inf;

    rep(i, 1, K + 1)

    {

        if (pos + i <= N - 1)

        {

            chmin(res, abs(h[pos] - h[pos + i]) + Solve(pos + i));

        }

    }

    return dp[pos] = res;

}



void _main()

{

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

    cin >> N >> K;

    h.resize(N);

    rep(i, 0, N) cin >> h[i];

    cout << Solve(0) << endl;

}


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

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

#define rrep(i,a,b) for(int i=a;i>=b;i--)

#define fore(i,a) for(auto &i:a)

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

//#pragma GCC optimize ("-O3")

using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }

typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------

void _main()

{

    int N, K;

    cin >> N >> K;

    vector<int> h(N);

    rep(i, 0, N) cin >> h[i];



    vector<int> dp(N + 1, inf);

    dp[0] = 0;

    rep(i, 0, N)

    {

        rep(j, 1, K + 1)

        {

            if (i + j >= N) continue;

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

        }

    }

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

}


// 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;





}