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

using namespace std;

 

#define pb push_back

#define ff first

#define ss second

#define endl '\n'

#define fast ios::sync_with_stdio(false); cin.tie(NULL);

const double eps = 1e-6;



/////////////////////////////////////////////



void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



////////////////////////////////



#define int long long

const int N = 1e5 + 10;

int n,k;

int a[N];

int dp[N];



int calc(int pos){



    if(pos < 0)

        return 1e10;

    if(pos==0)

        return 0;



    int &ans = dp[pos];



    if(ans!=-1)

        return ans;

    ans = 1e17;

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



        ans = min(ans , abs(a[pos]-a[pos-i]) + calc(pos-i));

    }



    return ans;





}



void solve(){



    cin>>n>>k;

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

        cin>>a[i];



    memset(dp , -1,sizeof dp);



    int ans = calc(n-1);



    cout<<ans<<endl;

}

signed main(){



    fast;

    int t = 1;

    //cin>>t;

    while(t--)

        solve();

}


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

using namespace std;

 

#define pb push_back

#define ff first

#define ss second

#define endl '\n'

#define fast ios::sync_with_stdio(false); cin.tie(NULL);

const double eps = 1e-6;







/////////////////////////////////////////////



void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



////////////////////////////////



#define int long long







void solve(){



	int n , k;

	cin>>n>>k;

	vector<int> a(n+2);

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

		cin>>a[i];



	int dp[n+2];



	dp[0] = 0;

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

		dp[i] = 1e12;

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



			if(i-j < 0)

				break;



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

		}

	}



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





}

signed main(){



    fast;

    int t = 1;

    //cin>>t;

    while(t--)

        solve();

}




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





}