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

 

typedef long long ll;

#define int ll

#define fi first

#define se second

#define SORT(a) sort(a.begin(),a.end())

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

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

#define MP(a,b) make_pair(a,b)

#define pb(a) push_back(a)

#define INF LLONG_MAX/2

 

using namespace std;

int MOD = 1000000007;

 

signed main(){



    int n,k;

    cin >> n >> k;

    int h[100010] = {0};

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



    int dp[100010] = {0};



    dp[1] = abs(h[1] - h[0]);

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

        dp[i] = abs(h[i]-h[i-1])+dp[i-1];

        for(int j = 2;j <= k  && i-j >= 0;j++){

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

        }

    }



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

    



    return 0;   

}



// g++ -std=c++14 code1.cpp

// rm -r -f test;oj dl https://dp.contest.atcoder.jp/tasks/dp_a

// rm -r -f test;oj dl http://yahoo-procon2017-qual.contest.atcoder.jp/tasks/yahoo_procon2017_qual_c

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

 

using namespace std;

typedef long long ll;

typedef vector<ll> vll;

#define int ll

#define fi first

#define se second

#define SORT(a) sort(a.begin(),a.end())

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

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

#define MP(a,b) make_pair(a,b)

#define pb(a) push_back(a)

#define INF LLONG_MAX/2

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

#define debug(x) cerr<<#x<<": "<<x<<endl

#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl

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



// int MOD = 998244353;

int MOD = 1000000007;





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

signed main(){



	ll n,k;

	cin >> n >> k;

	vll h(n+k+1);

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



	vll c(n+k+1,INF);

	c[0] = 0;

	rep(i,n){

		rep(j,k){

			c[i+j+1] = min(c[i+j+1],c[i]+abs(h[i]-h[i+j+1]));

		}

	}

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



	return 0;

}



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



// g++ -std=c++14 code1.cpp

// rm -r -f test;oj dl https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c

// rm -r -f test;oj dl https://dp.contest.atcoder.jp/tasks/dp_a

// rm -r -f test;oj dl http://arc092.contest.atcoder.jp/tasks/arc092_b

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





}