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

using namespace std;



#define ll  long long

#define pb push_back

#define mp make_pair

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

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

#define F first

#define S second

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

#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)

typedef pair<int, int>	pii;

typedef pair<ll, ll>	pl;

typedef vector<int>		vi;

typedef vector<ll>		vl;

typedef vector<pii>		vpii;

typedef vector<pl>		vpl;

typedef vector<vi>		vvi;

typedef vector<vl>		vvl;

const int mod = 1000000007;

int h[100001];

int dp[100001];

int k;



int go(int n){

    if(n<=0) return INT_MAX;

    else if(n==1)return 0;

    else if(n==2) return abs(h[2]-h[1]);

    else {

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

            return dp[n];

        }else{

            int mi=INT_MAX;

            //dp[n]=min(go(n-1)+abs(h[n-1]-h[n]),go(n-2)+abs(h[n-2]-h[n]));

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

               if(n-i>0) mi=min(mi,go(n-i)+abs(h[n-i]-h[n]));

                



            }

            dp[n]=mi;

            return dp[n];

        }  



    }  

}





int main() {

    

    int t, n, m;

    cin>>n>>k;

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

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



    Fo(i,n){

        cin>>h[i];

    }

    dp[1]=0;

    cout<<go(n)<<endl;

    return 0;

}






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

using namespace std;



#define ll  long long

#define pb push_back

#define mp make_pair

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

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

#define F first

#define S second

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

#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)

typedef pair<int, int>	pii;

typedef pair<ll, ll>	pl;

typedef vector<int>		vi;

typedef vector<ll>		vl;

typedef vector<pii>		vpii;

typedef vector<pl>		vpl;

typedef vector<vi>		vvi;

typedef vector<vl>		vvl;

const int mod = 1000000007;



int main() {

    

    int t, n, k;

    cin>>n>>k;

    int dp[n+1]={};

    

//Fo(i,n)cout<<dp[n]<<"  ";

//cout<<endl;

    int h[n+1];

    Fo(i,n) cin>>h[i];

    dp[1]=0;

    dp[0]=0;

    Fo(i,n){

        if(i==1) continue;

        int mi =INT_MAX;

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

            if((i-j)>0) mi=min(mi,dp[i-j]+abs(h[i-j]-h[i]));

        }

        dp[i]=mi;

        

    }

    cout<<dp[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;





}