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

using namespace std;

#define mod 1000000007

#define ll long long int

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);

#define inf 9223372036854775807

#define mini 9223372036854775807

#include <ext/pb_ds/assoc_container.hpp> 

#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 

#define ordered_set tree<double, null_type,less<double>, rb_tree_tag,tree_order_statistics_node_update>

pair<ll,ll>s4[4]={{-1,0},{1,0},{0,-1},{0,1}};

pair<ll,ll>s8[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,1},{1,0},{1,-1}};

ll power(ll a,ll b)

{

	if(b==0)

		return 1;

	ll c=power(a,b/2);

	if(b%2==0)

		return ((c%mod)*(c%mod))%mod;

	else

		return ((((c%mod)*(c%mod))%mod)*a)%mod;

}

ll n,k;

ll a[200005];

ll dp[200005];

ll func(ll i)

{

	if(dp[i]!=-1)

		return dp[i];

	ll ans1=inf;

	ll ans2=inf;

	if(i==n-1)

		return 0;

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

	{

		if((i+j)<n)

			ans1=abs(a[i+j]-a[i])+func(i+j);

		ans2=min(ans1,ans2);

	}

	dp[i]=min(ans1,ans2);

	return dp[i];

}

int main()

{

	boost

	cin>>n>>k;

	ll i;

	for(i=0; i<200005; i++)

		dp[i]=-1;

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

		cin>>a[i];

	ll ans=func(0);

	cout<<ans<<endl;

	return 0;

}

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

using namespace std;

#define mod 1000000007

#define ll long long int

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);

#define inf 9223372036854775807

#define mini 9223372036854775807

#include <ext/pb_ds/assoc_container.hpp> 

#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

pair<ll,ll>s4[4]={{-1,0},{1,0},{0,-1},{0,1}};

pair<ll,ll>s8[8]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,1},{1,0},{1,-1}};

ll power(ll a,ll b)

{

	if(b==0)

		return 1;

	ll c=power(a,b/2);

	if(b%2==0)

		return ((c%mod)*(c%mod))%mod;

	else

		return ((((c%mod)*(c%mod))%mod)*a)%mod;

}

ll arr[200005];

ll dp[200005];

int main()

{

	boost

	ll n,k;

	cin>>n>>k;

	ll i;

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

		cin>>arr[i];

	dp[0]=0;

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

	{

		dp[i]=inf;

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

		{

			if(i-j>=0)

			{

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

			}

		}

	}

	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;





}