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

#include<ext/pb_ds/assoc_container.hpp>



using namespace std;

using namespace __gnu_pbds;

//#define int          long long int

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

#define mod      1000000007

#define MOD      998244353

#define mp       make_pair

#define pb       push_back

#define F        first

#define S        second

#define t()      int test;cin>>test;while(test--)

#define ii       pair<int,int>

#define endl     "\n"

#define que_max  priority_queue <int>

#define IOS      ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);



typedef tree < int ,  null_type ,  greater <int> ,  rb_tree_tag ,  tree_order_statistics_node_update > pbds;



int pw(int b, int p) { int r = 1; while (p > 0) { if (p & 1) r = r * b;

        p = p >> 1; b = b * b; } return r; }





int n,k;

vector <int> arr(1e5);

vector <int> dp(1e5 , -1);



int solve(int id)

{

    if(id == n-1)

        return 0;



    if(dp[id]!=-1)

    {

        return dp[id];

    }

    int ans = INT_MAX;



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

        if(id + i >= n) break;

        ans = min(ans, solve(id + i) + abs(arr[id + i] - arr[id]));

    }



        return dp[id] = ans;

}

int main()

{

    cin>>n>>k;



    arr.resize(n);

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

    {

        cin>>arr[i];

    }

    cout<<solve(0)<<endl;

}


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

#include<ext/pb_ds/assoc_container.hpp>



using namespace std;

using namespace __gnu_pbds;

#define ll 		 long long int

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

#define mod 	 1000000007

#define MOD      998244353

#define mp       make_pair

#define pb       push_back

#define F        first

#define S        second

#define t()      ll test;cin>>test;while(test--)

#define ii       pair<int,int>

#define endl     "\n"

#define que_max  priority_queue <int>

#define IOS      ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);



typedef tree < int ,  null_type ,  greater <int> ,  rb_tree_tag ,  tree_order_statistics_node_update > pbds;



int pw(int b, int p) { int r = 1; while (p > 0) { if (p & 1) r = r * b;

        p = p >> 1; b = b * b; } return r; }





const int INF = 1e9+5;

int main()

{

	ll n , k;

	cin>>n>>k;

	vector <ll> v(n);

	for(auto &i : v)

		cin>>i;



	vector <ll> dp(n,INF);

	dp[0] = 0;



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

	{

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

		{

			if(j<n)

			{

				dp[j] = min(dp[j] , dp[i]+abs(v[i]-v[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;





}