// Original C++ solution (slower):
/**

 *      purpose : 

 *      author  : kyomukyomupurin

 *      created : 

**/



// input/output

#include <iostream>

#include <fstream>

#include <sstream>

// container class

#include <array>

#include <deque>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <unordered_map>

#include <unordered_set>

#include <vector>

// math, algorithm

#include <algorithm>

#include <cmath>

#include <complex>

#include <numeric>

// etc

#include <bitset>

#include <cassert>

#include <cctype>

#include <cstring>

#include <chrono>

#include <iomanip>

#include <random>

#include <utility>

// using-directive

using namespace std;

// alias template

using int64 = int64_t;

using pii = pair<int, int>;

using pll = pair<int64_t, int64_t>;

// text macro replacement

#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 print(x) cout << (x) << '\n'

#define debug(x) cout << #x << ": " << (x) << '\n'

// variadic template

template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}

template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}



const int INF = 1e9;



int main(){

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);



  int n, k; cin >> n >> k;

  vector<int> h(n);

  for (int i = 0; i < n; ++i) cin >> h[i];



  int dp[101010];

  fill(dp, dp + 101010, INF);

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



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

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

      if (i - step < 0) break;

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

    }

  }



  print(dp[n - 1]);



  return 0;

}

// Optimized solution to the same problem (faster):
/**

 *      author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏

 *      created : 2019-11-22:17:27:25

**/



#include <iostream>

#include <deque>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <vector>

#include <algorithm>

#include <cmath>

#include <complex>

#include <numeric>

#include <bitset>

#include <cassert>

#include <cctype>

#include <functional>

#include <iomanip>

#include <random>

#include <utility>



using namespace std;



using int64 = long long;

#define debug(_) cerr << #_ << ": " << (_) << '\n'

template<typename T> inline void chmin(T &a, T b) { if (a > b) a = b; return; }

template<typename T> inline void chmax(T &a, T b) { if (a < b) a = b; return; }

template<typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { os << '{'; for (size_t i = 0; i < vec.size(); ++i) { os << vec[i]; if (i != vec.size() - 1) os << ','; } os << '}'; return os; }

template<typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { return os << '{' << p.first << " " << p.second << '}'; }



constexpr int INF = 2e9;



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);



  int n; cin >> n; int k; cin >> k;

  vector<int> h(n);

  for (int i = 0; i < n; ++i) cin >> h[i];



  vector<int> dp(n + 5, INF);

  dp[0] = 0;



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

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

      if (i - j >= 0) {

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

      }

    }

  }

  //debug(dp);



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



  return 0;

}

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





}