// Original C++ solution (slower):
#include <array>

#include <cstdlib>

#include <cmath>

#include <climits>

#include <cfloat>

#include <map>

#include <utility>

#include <set>

#include <iostream>

#include <memory>

#include <string>

#include <vector>

#include <algorithm>

#include <functional>

#include <sstream>

#include <deque>

#include <complex>

#include <stack>

#include <queue>

#include <cstdio>

#include <cctype>

#include <cstring>

#include <ctime>

#include <iterator>

#include <bitset>

#include <numeric>

#include <list>

#include <iomanip>

#include <tuple>

using namespace std;





typedef long long LL;

typedef pair<int,int> pii;

typedef pair<LL,LL> pll;



typedef vector<int> vint;

typedef vector<vector<int> > vvint;

typedef vector<long long> vll, vLL;

typedef vector<vector<long long> > vvll, vvLL;



#define VV(T) vector<vector< T > >



template <class T>

void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){

	v.assign(a, vector<T>(b, t));

}



template <class F, class T>

void convert(const F &f, T &t){

	stringstream ss;

	ss << f;

	ss >> t;

}



template <class T>

T clampMax(const T& a, const T& b) { return min(a, b); }



template <class T>

T clampMin(const T& a, const T& b) { return max(a, b); }





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

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

#define RALL(v) (v).rbegin(),(v).rend()

#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

#define PB push





#define MOD 1000000007LL

#define EPS 1e-10



const int MAX_N = 100010;

const int INF_COST = 2000000000;

int N, K;

int h[MAX_N];

int memo[MAX_N];



int Solve(int pos)

{

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

    if (pos >= N) return memo[pos] = INF_COST;

    if (pos == N - 1) return memo[pos] = 0;



    int res = INF_COST;

    REP(i, K)

    {

        int nextPos = pos + (i + 1);

        res = min(res, Solve(nextPos) + abs(h[pos] - h[nextPos]));

    }

    return memo[pos] = res;

}



int main()

{

    cin >> N >> K;

    REP(i, N) cin >> h[i];

    REP(i, MAX_N) memo[i] = -1;

    cout << Solve(0) << endl;

    return 0;

}

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





}