// Original C++ solution (slower):
#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 ll long long

#define ld long double

typedef vector <ll> vi;                 // Vector of long long

typedef vector <vi> vvi;                // Vector of vi

typedef vector <pair<ll,ll>> ii;        // Vector of pairs

#define ff first                        // For pairs

#define ss second                       // For pairs

#define sz(a) int((a).size())           // Function to determine size of any container

#define pb push_back                    // Pushback to vector

#define mp make_pair                    // Makes pairs to be stored as pair

#define all(c) (c).begin(), (c).end()   // Mainly used by me in sorting

// ordered_set adds two new functions to set - (set).find_by_order([kth element based on zero indexing]) and order_of_key()

// order_of_key returns number of elements less that parameter. If element exists, that order is its index

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

#define MAX (ll)(1e5 + 10)

#define INF (ll)(1e10)



ll dp[MAX];

ll n, k;

vi h;



ll solve(ll i)

{

    if (i < 0)

        return INF;

    

    if (dp[i] == -1)

    {

        ll ans = INF;

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

            ans = min(ans, solve(i - j) + llabs(h[i] - h[i - j]));

        dp[i] = ans;

    }

    return dp[i];

}



int main(void)

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    cin >> n >> k;

    h.resize(n);

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

        cin >> h[i];

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

    dp[0] = 0;



    cout << solve(n - 1) << "\n";

}


// Optimized solution to the same problem (faster):
// Math = ♥️



#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 ll long long                  // Short form for long long

#define ld long double                // Short form for long double

typedef pair<ll, ll> ii;              // Pair of long long

typedef vector<ll> vi;                // Vector of long long

typedef vector<vi> vvi;               // Vector of vector of long long

typedef vector<ii> vii;               // Vector of pairs

typedef vector<vii> vvii;             // Vector of vector of pairs

#define pq priority_queue             // Max heap (To convert to min heap, use negative sign before every value)

#define ff first                      // For pairs

#define ss second                     // For pairs

#define pb push_back                  // Pushback to vector

#define mp make_pair                  // Makes pairs to be stored as pair

#define all(c) (c).begin(), (c).end() // Mainly used by me in sorting

// ordered_set adds two new functions to set - (set).find_by_order([kth element based on zero indexing]) and order_of_key()

// order_of_key returns number of elements less that parameter. If element exists, that order is its index

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

/*-------------------------------------------------------------------------------------------------------------------------*/



int main(void)

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    ll n, k;

    cin >> n >> k;



    vi arr(n);

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

        cin >> arr[i];



    ll dp[n];

    dp[0] = 0;

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

    {

        dp[i] = dp[i - 1] + llabs(arr[i] - arr[i - 1]);

        for (int j = 2; i - j >= 0 && j <= k; j++)

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

    }



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

}


// 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;





}