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

using namespace std;

using ll = long long;

using vi = vector<int>;

using vc = vector<char>;

using vb = vector<bool>;

using vd = vector<double>;

using vs = vector<string>;

using vll = vector<long long>;

using vpii = vector<pair<int, int>>;

using vvi = vector<vector<int>>;

using vvc = vector<vector<char>>;

using vvs = vector<vector<string>>;

using vvb = vector<vector<bool>>;

using pii = pair<int, int>;

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

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

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

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

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

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

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

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

#define call(x) (x).cbegin(), (x).cend()

#define MAX(x) *max_element(all(x))

#define MIN(x) *min_element(all(x))

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

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

const int INF = INT_MAX;

const int MOD = 1000000007;

const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};



int main() {

    int N, K;

    cin >> N >> K;

    vi h(N);

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



    vll dp(100500, INF);

    dp[0] = 0;

    FOR(i, 0, N-2) {

        reps(j, K) {

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

        }

    }

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

}

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

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

#define fi first

#define se second

#define pb push_back

#define eb emplace_back

#define sz(x) (int)(x).size()

using namespace std;

using ll = long long;

using P = pair<int, int>;

using vi = vector<int>;

using vc = vector<char>;

using vb = vector<bool>;

using vs = vector<string>;

using vll = vector<long long>;

using vp = vector<pair<int, int>>;

using vvi = vector<vector<int>>;

using vvc = vector<vector<char>>;

using vvll = vector<vector<long long>>;

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

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



const int INF = 1001001001;



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    

    int n, k;

    cin >> n >> k;

    vi h(n);

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



    vi dp(n+k, INF);

    dp[0] = 0;

    rep(i, n) {

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

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

        }

    }

    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;





}