// Original C++ solution (slower):
// Created by Kshitij Anand NSIT

#include <bits/stdc++.h>





using namespace std;





#define int long long

#define pb push_back

#define P pair<int,int>

#define F first

#define S second

#define vi vector<int>

#define vc vector<char>

#define vb vector<bool>

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

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

#define mp(a, b) make_pair(a, b)

#define min3(a, b, c) min(min(a, b), c)

#define min4(a, b, c, d) min(min(a, b), min(c, d))

#define max3(a, b, c) max(max(a, b), c)

#define max4(a, b, c, d) max(max(a, b), max(c, d))

#define fill(arr,val) memset(arr,val,sizeof(arr))

#define db(x)  cout<<#x<<" : "<<x<<endl





const int N = 1000000007;



int dp[100009];



int func(int arr[] , int n , int curr , int k){



	if(curr >= n){



		return 0;

	}



	if(curr == n-1)

		return 0;



	if(dp[curr] !=-1)  return dp[curr];



	int ans = INT_MAX;



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



		if(curr + i <n){



		int temp = func(arr , n , curr+i , k);

		if(temp!=-1)

			ans = min(ans , abs(arr[curr] - arr[curr+i])  + temp);

		}

	}



	



	if(ans == INT_MAX)

		ans = -1;



	dp[curr] = ans;

	return ans;



}



void solve(){



    int n,k,b,c, ans=0, count=0, sum=0;

    cin>>n>>k;

    int arr[n];

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

      cin>>arr[i];

    }

    fill(dp , -1);

    ans = func(arr , n , 0 , k);

    cout<<ans<<endl;

}



int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL); cout.tie(NULL);

    

    

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

    {     

      solve();

    }



  return 0;

}


// Optimized solution to the same problem (faster):
// Created by Kshitij Anand NSIT

#include <bits/stdc++.h>





using namespace std;





#define int long long

#define pb push_back

#define P pair<int,int>

#define F first

#define S second

#define vi vector<int>

#define vc vector<char>

#define vb vector<bool>

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

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

#define mp(a, b) make_pair(a, b)

#define min3(a, b, c) min(min(a, b), c)

#define min4(a, b, c, d) min(min(a, b), min(c, d))

#define max3(a, b, c) max(max(a, b), c)

#define max4(a, b, c, d) max(max(a, b), max(c, d))

#define fill(arr,val) memset(arr,val,sizeof(arr))

#define db(x)  cout<<#x<<" : "<<x<<endl





const int N = 1000000007;



int func(int arr[] ,  int n , int k){



	int dp[n];



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

		dp[i] = INT_MAX;

	}

	dp[0] = 0;



	dp[1] = abs(arr[1] - arr[0]);





	for(int i=2; i<n; i++) {



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



			if(i-j >=0){

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



			}

		}





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

	}



	return dp[n-1];



}



void solve(){



    int n,k,b,c, ans=0, count=0, sum=0;

    cin>>n>>k;

    int arr[n];

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

      cin>>arr[i];

    }



    ans = func(arr , n , k);



    cout<<ans<<endl; 

}



int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL); cout.tie(NULL);

    

    

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

    {     

      solve();

    }



  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;





}