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

using namespace std;

typedef long long int lli;

typedef vector <int> vi;

typedef vector <vi> vvi;

typedef pair <int,int> pr;

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

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

#define pb push_back

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

#define tr(c, i) for (typeof ((c).begin() i = (c).begin(); i != (c).end(); i++)

#define present(c, x)((c).find(x) != (c).end())

#define cpresent(c, x)(find(all(c), x) != (c).end())

// for mod power a^b%m use power<lli> (a,b,m)

template < class mytype >

mytype power(mytype a, mytype b, mytype m){if (b == 1) return a % m;if (b == 0) return 1;

lli root = power < lli > (a, b / 2, m);lli ans = (root * root) % m;if (b % 2) ans = (ans * a) % m;

return ans;

}

// for gcd use gcd<int> (a,b)

template < class type >type gcd(type a, type b){if (a == 0) return b;else return gcd(b % a, a);}

template < class type >type mod_inv(type a, type p) {return power(a, p - 2, p);}



// use from here bitches

int n, k;

vector<int> height(100002) , values(100002 , -1);

int dp(int x){

if(values[x] != -1) return values[x];

if(x >= n-1) return 0;int ans = INT_MAX;

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

	ans = min(ans , abs(height[x+j] - height[x]) + dp(x+j));

}

return values[x] = ans;

}





int main()

{    

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cin >> n >> k;

	fr(i , n){

		cin >> height[i];

	}

	cout << dp(0);

		



	





    return 0;

	

}


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

using namespace std;

typedef long long int lli;

typedef vector <int> vi;

typedef vector <vi> vvi;

typedef pair <int,int> pr;

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

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

#define pb push_back

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

#define tr(c, i) for (typeof ((c).begin() i = (c).begin(); i != (c).end(); i++)

#define present(c, x)((c).find(x) != (c).end())

#define cpresent(c, x)(find(all(c), x) != (c).end())

// for mod power a^b%m use power<lli> (a,b,m)

template < class mytype >

mytype power(mytype a, mytype b, mytype m){if (b == 1) return a % m;if (b == 0) return 1;

mytype root = power < lli > (a, b / 2, m);mytype ans = (root * root) % m;if (b % 2) ans = (ans * a) % m;

return ans;

}

// for gcd use gcd<int> (a,b)

template < class type >type gcd(type a, type b){if (a == 0) return b;else return gcd(b % a, a);}

template < class type >type mod_inv(type a, type p) {return power(a, p - 2, p);}





int main()

{

    int n , k;

    cin >> n >> k;

    vector<int> h(n) , cost(n , 1e9+7);

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

    cost[0] = 0;

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

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

            if(j < n) cost[j] = min(cost[j] , cost[i] + abs(h[i] - h[j]));

        }

    }

    cout << cost[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;





}