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

#include <string>

#include <vector>

#include <algorithm>

#include <stack>

#include <queue>

#include <cmath>

#include <tuple>

#include <cstdio>

#include <bitset>

#include <sstream>

#include <iterator>

#include <numeric>

#include <map>

#include <cstring>



using namespace std;



//#define DEBUG_ //!!提出時にコメントアウト!!

#ifdef DEBUG_

	#define dump(x)  cerr << #x << " = " << (x) << endl;

#else

	#define dump(x)  ; //何もしない文

#endif



#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n)  FOR(i,0,n)

#define SZ(x) ((int)(x).size()) //unsignedのサイズをint型に変換



typedef vector<int> VI;

typedef vector<VI> VVI;

typedef vector<string> VS;

typedef pair<int, int> PII;

typedef long long LL; 



template <typename T>

std::string printVector(const std::vector<T> &data)

{

    std::stringstream ss;

    std::ostream_iterator<T> out_it(ss, ", ");

    ss << "[";

    std::copy(data.begin(), data.end() - 1, out_it);

    ss << data.back() << "]";

    return ss.str();

}



const int MOD = 1e9+7;

const int INF = 1e9+7;

const int MAX_N = 1e5+10;

int dp[MAX_N];



//ここから書き始める

int main(int argc, char const *argv[])

{

    cin.tie(0);

    ios::sync_with_stdio(false);	

    int N,K;

    cin >> N >> K;

    VI h(N);

    REP(i,N)

    {

        cin >> h[i];

    }



    memset(dp, 0, sizeof(dp));



    for(int i=N-1; i>0; i--)

    {

        int tmp=INF;

        FOR(j,1,K+1)

        {

            dump(i)

            dump(j)

            if(i+j > N) break;

            int kouho = dp[i+j] + abs(h[i-1] - h[i-1+j]);

            dump(kouho)

            if(kouho < tmp) tmp = kouho;

        }

        dp[i] = tmp;

        dump(i)

        dump(dp[i])

    }



    cout << dp[1] << endl;







}


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

#include <string>

#include <vector>

#include <algorithm>

#include <stack>

#include <queue>

#include <cmath>

#include <tuple>

#include <cstdio>

#include <bitset>

#include <sstream>

#include <iterator>

#include <numeric>

#include <map>

#include <cstring>

#include <set>

#include <functional>

#include <iomanip>



using namespace std;



#define DEBUG_ //!!提出時にコメントアウト!!

#ifdef DEBUG_

	#define dump(x)  cerr << #x << " = " << (x) << endl;

#else

	#define dump(x)  ;

#endif



#define equals(a,b) (fabs((a)-(b)) < EPS)

#define FOR(i,a,b) for(int i=(a);i<(b);++i)

#define REP(i,n)  FOR(i,0,n)

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

#define pb push_back

#define eb emplace_back



//#define int long long



typedef long long LL;

typedef vector<int> VI;

typedef vector<LL> VL;

typedef vector<VI> VVI;

typedef vector<string> VS;

typedef pair<int, int> PII;

typedef pair<LL, LL> PLL;



template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }



template <typename T>

std::string printVector(const std::vector<T> &data)

{

    std::stringstream ss;

    std::ostream_iterator<T> out_it(ss, ", ");

    ss << "[";

    std::copy(data.begin(), data.end() - 1, out_it);

    ss << data.back() << "]";

    return ss.str();

}



template <typename T>

void print_array(const T &ary, int size){

    REP(i,size){

        cout << ary[i] << " ";

    }

    cout << endl;

}



const int MOD = 1e9+7;

const LL LINF = 1001002003004005006ll;

const int INF = 1001001001;

const double EPS = (1e-10);

const int MAX_N = 1e5+10;



LL dp[MAX_N];



signed main(int argc, char const *argv[])

{

    cin.tie(0);

    ios::sync_with_stdio(false);

    cout << setprecision(12);



    int N,K; cin >> N >> K;

    VL h(N);

    REP(i,N) cin >> h[i];

    REP(i,N){

        dp[i] = LINF;

    }

    dp[0] = 0;

    REP(i,N){

        for(int j = i+1; j <= min(i+K,N-1); j++){

            chmin(dp[j], dp[i]+abs(h[j]-h[i]));

        }

    }

    cout << dp[N-1] << endl;





}


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





}