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

using namespace std;



#define ll             long long

#define LL             long long

#define gcd(a,b)       __gcd(a, b)

#define lcm(a,b)       a * (b / gcd(a, b))

#define pii            pair<int, int>

#define pll            pair<ll, ll>

#define pil            pair<int, ll>

#define pli            pair<ll, int>

#define vi             vector<int>

#define vl             vector<ll>

#define vii            vector<pii>

#define vll            vector<pll>

#define vil            vector<pil>

#define vli            vector<pli>

#define pb             push_back

#define ppb            pop_back

#define mp             make_pair

#define ff             first

#define ss             second

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

#define fill(a, b)     memset(a, b, sizeof a)

#define smax(a, b)     a = max(a, b)

#define smin(a, b)     a = min(a, b)

#define sqr(x)         x * x

#define cube(x)        x * x * x

#define endl           '\n'



int in() {

	int n;

	scanf("%d", &n);

	return n;

}



ll Lin() {

	ll n;

	scanf("%lld", &n);

	return n;

}



double Din() {

	double n;

	scanf("%lf", &n);

	return n;

}



const ll inf = (ll)1e17;

const ll mod = (ll)1e9 + 7;

const int N = 1e5 + 5;



ll dp[N], a[N];

int n, k;



ll call(int pos) {

	if (pos < 1) return 2e9;

	if (pos == 1) return 0;

	if (dp[pos] != -1) return dp[pos];

	ll ans = 2e9;

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

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

	}

	return dp[pos] = ans;

}





int solve() {

	n = in(), k = in();

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

		a[i] = Lin();

	}

	fill(dp, -1);

	printf("%lld\n", call(n));

	return 0;

}



int main() {

	int test = 1, tc = 0;

	while (test--) {

		//printf("Case %d: ", ++tc);

		solve();

	}

	return 0;

}


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

using namespace std;



#define ll       long long

#define LL       long long

#define ull      unsigned ll

#define pii      pair<int, int>

#define pll      pair<ll, ll>

#define mp       make_pair

#define ff       first

#define ss       second

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

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



template<typename T> inline void smin(T &a, T b) {

    a = a < b ? a : b;

}

template<typename T> inline void smax(T &a, T b) {

    a = a > b ? a : b;

}



template<typename T> inline void Int(T &n) {

    n = 0;

    int f = 1;

    register int ch = getchar();

    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;

    for(; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';

    n *= f;

}

template<typename T, typename TT> inline void Int(T &n, TT &m) {

    Int(n), Int(m);

}

template<typename T, typename TT, typename TTT> inline void Int(T &n, TT &m, TTT &l) {

    Int(n), Int(m, l);

}



#define error(args...) { string _s = #args; replace(all(_s), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {

    cout << endl;

}

template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {

    cerr << *it << " = " << a << ", ";

    err(++it, args...);

}



const int mod = (int) 1e9 + 7;



int add(int a, int b) {

    a += b;

    if(a >= mod) a -= mod;

    return a;

}

int sub(int a, int b) {

    a -= b;

    if(a <= 0) a += mod;

    return a;

}

int mul(int a, int b) {

    return (ll) a * b % mod;

}



const int inf = (int) 2e9 + 5;

const ll  Inf = (ll) 2e18 + 5;

const int   N = (int) 2e5 + 5;









int solve() {

    int n, k; Int(n, k);

    vector<int> dp(n + 1, inf);

    dp[1] = 0;

    vector<int> h(n + 1);

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

        Int(h[i]);

    }

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

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

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

        }

    }

    printf("%d\n", dp[n]);

    return 0;

}



int main() {

    int test = 1, tc = 0;

    //Int(test);

    while(test--) {

        //printf("Case %d: ", ++tc);

        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;





}