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

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

#define SORT(v, n) sort(v, v+n);

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll int64_t

#define pb(a) push_back(a)

#define INF 1000000000

#define MOD 1000000007

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;



typedef vector<unsigned int>vec;

typedef vector<vec> mat;



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

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





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

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



ll dp[101010];

ll h[101010];



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    int n,K;

    cin>>n>>K;

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

    REP(i,101010) dp[i]=1LL<<60;

    dp[0]=0;

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

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

            chmin(dp[i+k],dp[i]+abs(h[i+k]-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 < n;i++)

#define VSORT(v) sort(v.begin(), v.end())

#define VRSORT(v) sort(v.rbegin(), v.rend())

#define ll long long

using namespace std;

typedef pair<int, int> P;

typedef pair<ll, ll> LP;

typedef pair<int, P> PP;

typedef pair<ll, LP> LPP;

//typedef vector<unsigned int>vec;

typedef vector<ll>vec;

typedef vector<vec> mat;

typedef vector<vector<int>> Graph;



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

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

const int INF = 1000000000;

const ll LINF = 1000000000000000000;//1e18

const ll  MOD = 1000000007;

const double PI = acos(-1.0);

const double EPS = 1e-10;



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

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

template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};



int h[202020];

ll dp[202020];

int N, K;



ll rec(int i){

    if(dp[i] < LINF) return dp[i];

    if(i <= 0) return 0;



    ll res = INF;

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

        if(i - k >= 0) chmin(res, rec(i-k) + abs(h[i] - h[i-k]));

    }



    return dp[i] = res;

}



int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    cin >> N >> K;

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

    REP(i,N) dp[i] = LINF;



    cout << rec(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;





}