// Slower C++ solution:
    #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;

    }

// Faster C++ solution for the same problem (measured speedup: 1.705470920865138x):
#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;

}