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

using namespace std;

#define gcd(a,b) __gcd(a,b)

#define ll long long int

#define ld unsigned long long int

#define pi pair<ll,ll>

#define pb push_back

#define pf push_front

#define pu push

#define po pop

#define fi first

#define se second

#define mk make_pair

#define ve vector

#define lr(n) for(ll i=0;i<n;i++)

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



ll dp[100001];

ve<ll>ar(100001);



ll fun(ll n,ll k){

	if(n==0){

		return 0;

	}

	if(dp[n]==-1){

		dp[n]=abs(ar[n]-ar[n-1])+fun(n-1,k);

		for(ll i=1;i<k;i++)

			if(n>i){

				dp[n]=min(dp[n],abs(ar[n]-ar[n-i-1])+fun(n-i-1,k));

			}

			else{

				break;

			}

	}

	return dp[n];

}



	int main(){

		ios_base::sync_with_stdio(0);

		cin.tie(0);

		cout.tie(0);

		ll n,k;

		cin>>n>>k;

		lr(n){

			cin>>ar[i];

		}

		lr(100001){

			dp[i]=-1;

		}

		cout << fun(n-1,k) << "\n";

		return 0;

	}

// Optimized solution to the same problem (faster):
// #pragma GCC optimize("Ofast")

// #pragma GCC optimize ("unroll-loops")

// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

 

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace __gnu_pbds;

using namespace std;

#define gcd(a,b) __gcd(a,b)

#define ll long long int

#define ld unsigned long long int

#define pi pair<ll,ll>

#define pb push_back

#define pf push_front

#define pu push

#define po pop

#define fi first

#define se second

#define mk make_pair

#define ve vector

#define lr(n) for(ll i=0;i<n;i++)

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

#define be begin

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

#define INF 1e18

 

typedef tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// order_of_key(val): returns the number of values less than val

// find_by_order(k): returns an iterator to the kth largest element (0-based)

 

// ll mod=998244353;

// ll gcd(ll a, ll b){

//     if(a<b) swap(a,b);

//     if(b==0) return a;

//     return gcd(b,a%b);

// }

 

// ll fun(ll x, ll y){

//     ll rem=1;

//     while(y!=0){

//         if(y%2==1){

//             rem=(rem*x)%mod;

//         }

//         x=(x*x)%mod;

//         y/=2;

//     }

//     return rem;

// }



int main(){

	ios_base::sync_with_stdio(0);

	cin.tie(0);

	cout.tie(0);

	ll n,k;

	cin>>n>>k;

	ve<ll>ar(n+1);

	lr(n){

		cin>>ar[i+1];

	}

	ll dp[n+1]={0};

	for(ll i=n-1;i>=1;i--){

		dp[i]=INF;

		for(ll j=i+1;j<=min(n,i+k);j++){

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

		}

	}

	cout << dp[1] << "\n";

	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;





}