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

#include <iostream>

#include <vector>

#include <queue>

#include <set>

#include <string>

#include <map>

#include <cmath>

#include <cstring>



#define fi first

#define se second

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

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

#define drep(i,n) for (int i = (n)-1; i >= 0; --i)

#define srep(i,s,t) for (int i = s; i < t; ++i)

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

#define maxs(x,y) (x = max(x,y))

#define mins(x,y) (x = min(x,y))

#define pb push_back

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

#define PQ(T) priority_queue<T, v(T), greater<T> >



using namespace std;

typedef long long ll;

typedef unsigned uint;

typedef unsigned long long ull;

typedef pair<int,int> P;



const ll LINF = 1001002003004005006ll;

const int INF = 1001001001;



const int mod = 1000000007;

const int MAXN = 101010;

int dp[MAXN];

int main() {

    int N, K;

    cin >> N >> K;

    vector<int> h(N);

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

    

    rep(i,MAXN) dp[i] = INF;



    dp[0] = 0;

    rep(i,N) {

        srep(j, 1, K+1) {

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

        }

    }

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

    return 0;

}


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

#include <iostream>

#include <vector>

#include <queue>

#include <set>

#include <string>

#include <map>

#include <cmath>

#include <cstring>



#define fi first

#define se second

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

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

#define drep(i,n) for (int i = (n)-1; i >= 0; --i)

#define srep(i,s,t) for (int i = s; i < t; ++i)

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

#define maxs(x,y) (x = max(x,y))

#define mins(x,y) (x = min(x,y))

#define pb push_back

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

#define PQ(T) priority_queue<T, v(T), greater<T> >



using namespace std;

typedef long long ll;

typedef unsigned uint;

typedef unsigned long long ull;

typedef pair<int,int> P;



const ll LINF = 1001002003004005006ll;

const int INF = 1001001001;



const int mod = 1000000007;



int main() {

    int n, k;

    cin >> n >> k;

    vector<int> h(n);

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

    int dp[100100];

    rep(i,n) dp[i] = INF;

    

    dp[0] = 0;

    rep(i,n) {

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

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

        }

    }

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

    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;





}