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





using namespace std;

long long n;

long long k;

vector<long long> vec;



long long meme[100007];



long long solve(long long index){



    if(index==n-1){

        return 0;

    }

    

    if(meme[index]!=-1){

        return meme[index];

    }

    

    long long ret = 10000000000000000;

    long long sol = 10000000000000000;

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

        if(index+i<n){

            ret = abs (vec[index]- vec[index+i]) + solve(index+i); 

        }

        sol = min(sol,ret);

    }



    return meme[index] = sol;

}



int main(){

    cin>>n;

    cin>>k;

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

    long long num;

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

        cin>>num;

        vec.push_back(num);

    }

    cout<<solve(0)<<endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<iomanip>

#include<cstdio>

#include<cmath>

#include<vector>

#include<string>

#include<algorithm>

#include<queue>

#include<set>

#include<map>

#include<limits>

#include<stack>



using namespace std;



typedef unsigned long long ll;

typedef pair<int,int> ii;

typedef vector<pair<int,int> > vii;

typedef vector<vii> vvii;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vll;

typedef vector<vll> vvll;

typedef vector<bool> vb;

typedef vector<vb> vvb;

typedef vector<char> vc;

typedef vector<vc> vvc;

typedef vector<string> vs;



int INF = std::numeric_limits<int>::max();

int NINF = std::numeric_limits<int>::min();

int MOD = 1e9+7;



int col[] = {0,-1,0,1,1,1,-1,-1};

int row[] = {-1,0,1,0,1,-1,1,-1};



//knight moves

int cc[] = {1,2,2,1,-1,-1,-2,-2};

int rr[] = {-2,-1,1,2,-2,2,1,-1};



int main()

{

    ios::sync_with_stdio(0);

    cin.tie(0);



    int n,k;

    cin >> n >> k;



    vi hight(n);

    vll cost(n,INF);



    cin >> hight[0];

    cost[0] = 0;



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

    {

        cin >> hight[i];



        if(i > k)

        {

            for(int j = i-1; j >= i-k ; --j)

            {

                ll one = abs(hight[i]-hight[j]);



                cost[i] = min(one+cost[j],cost[i]);

            }

            //cout << i << " " << cost[i] << endl;

        }

        else

        {

            cost[i] = abs(hight[0]-hight[i]);

        }

    }



    cout << cost[n-1];



    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;





}