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

using namespace std;

#define ll long long



const ll INF= 1LL << 60;



//入力

ll N;

ll K;

ll h[100010];

//dp

ll dp[100010];





void chmin(ll& a, ll b){

  if(a>b){

    a=b;

  }

}



int main(){

  cin >> N >> K;

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

  for(int i=0;i<100010;i++) dp[i]=INF;

  

  dp[0]=0;

  

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

    for(int j=i+1;j<=i+K;j++){

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

      //cout<<i<<","<<j<<":"<<dp[j]<<endl;

    }

  }

  

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

}

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



using namespace std;



#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++)

#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);i--)

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

#define REP2(i,n) rep2(i,0,n)

#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)

#define ll long long

#define ull unsigned long long

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

#define en '\n'

template <class T> using vec = vector<T>;

template <class T> using vvec = vector<vec<T>>;

typedef pair<ll, ll> P;

constexpr long long INF = 1LL << 60;

constexpr int INF_INT = 1 << 25;

constexpr long long MOD = (ll) 1e9 + 7;

typedef vector<ll> Array;

typedef vector<Array> Matrix;

constexpr int loose = 0;

constexpr int tight = 1;





template<class T> inline bool chmin(T& a, T b) {

    if (a > b) {

        a = b;

        return true;

    }

    return false;

}

template<class T> inline bool chmax(T& a, T b) {

    if (a < b) {

        a = b;

        return true;

    }

    return false;

}



struct Edge {

	ll to, cap, rev;

	Edge(ll _to, ll _cap, ll _rev) {

	to = _to; cap = _cap; rev = _rev;

	}

};

typedef vector<Edge> Edges;

typedef vector<Edges> Graph;



void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {

	G[from].push_back(Edge(to, cap, (ll)G[to].size()));

	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));

}



void solve(){

	ll n, k;

	cin>>n>>k;

	Array h(n);

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

	

	Array dp(n,INF);

	dp[0]=0;

	REP(i,n){

		rep(j,1,k+1){

			if(i+j>=n) break;

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

		}

	}

	cout<<dp[n-1]<<en;

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);

    cout.tie(0);



    solve();

    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;





}