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

     

    using namespace std;

     

    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 forn(i, n) for(int i = 0; i < n; i++)

    #define forw(i, w) for(int i = n; i >= 0; i--)

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

    #define dforw(i, b, a) for(int i = b; i >= a; i--)

    #define read(v) for(auto &i:v) cin >> i;

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

     

    typedef long long ll;

    typedef vector<int> vi;

    typedef vector<double> vd;

    typedef pair<int, int> pii;

     

    int n, k;

    vi arr, dp;

     

    int main(){

    	ios_base::sync_with_stdio(0);

    	cout.tie(0);cin.tie(0);

    	

    	cin >> n >> k;

     

    	arr.assign(n, 0);

    	dp.assign(n, 999999999);

    	dp[0] = 0;

    	read(arr);

     

    	forn(i, n)

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

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

     

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

    	return 0;

    }

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

#include <cstring>



using namespace std;



const int maxn = 1e5+5;

int n,K;

int h[maxn],f[maxn];



int abs(int x,int y) {

	if (x - y < 0) return y - x;

	return x - y;

}



int min(int x,int y) {

	if (x < y) return x;

	return y;

}



int main() {

	memset(f,0x3f,sizeof(f));

	scanf("%d %d",&n,&K);

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

		scanf("%d",&h[i]);

	}

	f[0] = f[1] = 0;

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

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

			f[i] = min(f[i-k]+abs(h[i],h[i-k]),f[i]);

		}

	}

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

	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;





}