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

 It's better to sleep happy and tired rather than disappointed and frustrated

 There's only 1 thing that's impossible and that is defeating a man who doesn't give up

*/

#include <bits/stdc++.h>

using namespace std;

#define fi first

#define se second

#define pb push_back

#define mx2 102

#define mx3 1003

#define mx4 10004

#define mx5 100005

#define mx6 1000006

#define mod 1000000007

#define PI 3.14159265

#define deb(x) cout << #x << " " << x << endl;

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

#define Fo(i,k,n) for(int i=k ; i<n ; i++)

 

typedef long long int ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef pair<pair<int,int>,int> piii;



const int inf=0x3f3f3f3f;

int n ,k, ans , sum , a , b ,arr[mx5],dp[mx5];

string str;



int solve(int i)

{

	if(i==n-1)

	{

		return 0;

	}

	if(i>n)

	{

		return inf;

	}

	if(dp[i]!=-1)

	{

		return dp[i];

	}

	int tmp=inf;

	Fo(j,1,k+1)

	{

		if(i+j<n)

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

	}

	return dp[i]=tmp;

}

 

int main() {

	ios::sync_with_stdio(false);

	//int t;cin>>t;while(t--)

	{

		cin>>n>>k;

		fo(i,n)cin>>arr[i];

		memset(dp,-1,sizeof(dp));

		cout<<solve(0);

	}

	return 0;

}


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

 It's better to sleep happy and tired rather than disappointed and frustrated

 There's only 1 thing that's impossible and that is defeating a man who doesn't give up

*/

#include <bits/stdc++.h>

using namespace std;

#define fi first

#define se second

#define pb push_back

#define mx2 102

#define mx3 1003

#define mx4 10004

#define mx5 100005

#define mx6 1000006

#define mod 1000000007

#define PI 3.14159265

#define deb(x) cout << #x << " " << x << endl;

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

#define Fo(i,k,n) for(int i=k ; i<n ; i++)

 

typedef long long int ll;

typedef pair<int,int> pii;

typedef pair<ll,ll> pll;

typedef pair<pair<int,int>,int> piii;



const int inf=0x3f3f3f3f;

int n ,k, ans , sum , a , b ,arr[mx5],dp[mx5];

string str;



 

int main() {

	ios::sync_with_stdio(false);

	//int t;cin>>t;while(t--)

	{

		cin>>n>>k;

		fo(i,n)cin>>arr[i];

		for(int i=n-2 ; i>=0 ; i--)

		{

			int t=inf;

			Fo(j,1,k+1)

			{

				if(i+j<n)

				{

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

				}

			}

			dp[i]=t;

		}

		cout<<dp[0];

	}

	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;





}