// Original C++ solution (slower):
#include <iostream>

#include <cmath>

#include <cstring>



using namespace std;



int pedras[100100];  ///Inicializo o vetor das pedras;

long long dp[100100]; ///Guardo os valores da funcao frog nesse vetor dp;



int n,k;



///Aqui comeca a funcão do frog2 top-down recursiva



int frog2(int i,int j){



    if(dp[i] != -1){

        return dp[i];

    }



    if(i>=j) return dp[i]=0;

    

    int menor = 1000000000;



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

        if(i+x <= j){  

            long long cost = (frog2(i+x,j)+ abs(pedras[i]-pedras[i+x]));

  

            if(cost < menor){ ///Acho o menor custo dentre as tentativas k;

                menor = cost;

            }



        }

    }



    return dp[i] = menor;

}

////////////////////////////////////////////////////////



int main(){



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



    cin >>n>>k;



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

        cin >> pedras[i];

    }



    cout<<frog2(0,n-1)<<endl;

}

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

using namespace std;



typedef long long ll;

typedef pair<int, int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

typedef vector<vi> vvi;



#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)

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

#define rall(x) (x).rbegin(), (x).rend()

#define ms(a, x) memset(a, x, sizeof(a))

#define len(x) (x).size()

#define pb push_back

#define eb emplace_back

#define f first

#define s second



const double eps = 1e-9;

const double pi = acos(-1.0);

const int inf = 1e9 + 7;

const int maxn = 1e5+5;



int n, k;

int h[maxn];

bool seen[maxn];

int dp[maxn];



int solve(int idx) {

    if (idx == n-1) return 0;

    if (seen[idx]) return dp[idx];

    seen[idx] = true;

    int ans = inf;

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

        if (idx + i >= n) break;

        ans = min(ans, solve(idx + i) + abs(h[idx] - h[idx + i]));

    }

    return dp[idx] = ans;

}



int main() {

    fastIO();

    cin >> n >> k;

    for (int i = 0; i < n; ++i) cin >> h[i];

    

    for (int idx = n-2; idx >= 0; --idx) {

        int &ans = dp[idx];

        ans = inf;

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

            if (i + idx >= n) break;

            ans = min(ans, dp[idx + i] + abs(h[idx] - h[idx + i]));

        }

    }



    cout << dp[0] << "\n";

    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;





}