#include <bits/stdc++.h>
using namespace std;

namespace cp {
	#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
	#define ROF(i, a, b) for (int i = (b); i >= (a); i--)
	#define REP(a) for (int i = 0; i < (a); i++)
	#define pb push_back
 
	typedef long long ll;
	typedef vector<int> vi;
	
	const ll INF = 9223372036854775807;
	const int MOD = 1e9 + 7;
	
	// to_string methods
	string ts(int n) { return to_string(n); }
	string ts(double n) { return to_string(n); }
	string ts(long double n) { return to_string(n); }
	string ts(long long n) { return to_string(n); }
	string ts(char c) { return to_string(c); }
	string ts(bool b) { return b ? "true" : "false"; }
	string ts(string s) { return s; }
	
	template <typename T, typename V>
	string ts(pair<T,V> p) {
		return "{" + ts(p.first) + ", " + ts(p.second) + "}";
	}
	
	template <typename T>
	string ts(T arr[], int len) {
		ostringstream oss;
		oss << "[";
		string delim = "";
		for (int i = 0; i < len; i++) {
		   oss << delim << ts(arr[i]);
		   delim = ", ";
		}
		oss << "]";
		return oss.str();
	}
	
	template<typename Container>
	string ts(const Container &v) {
		ostringstream oss;
		oss << "[";
		string delim = "";
		for (const auto &x : v) {
		   oss << delim << ts(x);
		   delim = ", ";
		}
		oss << "]";
		return oss.str();
	}
	
	template <typename T, typename V>
	string ts(map<T,V> m) {
		ostringstream oss;
		oss << "[";
		string delim = "";
		for (pair<T,V> p : m) {
		   oss << delim;
		   oss << "{" << p.first << ": " << p.second << "}";
		   delim = ", ";
		}
		oss << "]";
		return oss.str();
	}
	
	struct custom_hash {
	    static uint64_t splitmix64(uint64_t x) {
	        // http://xorshift.di.unimi.it/splitmix64.c
	        x += 0x9e3779b97f4a7c15;
	        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
	        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
	        return x ^ (x >> 31);
	    }
	    
	    size_t operator()(uint64_t x) const {
        	static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        	return splitmix64(x + FIXED_RANDOM);
    	}
	    
	    size_t operator()(pair<uint64_t,uint64_t>& x) const {
			static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
			return splitmix64(x.first + FIXED_RANDOM)^(splitmix64(x.second + FIXED_RANDOM) >> 1);
		}
	};
	
	// IO
	void setI(string s) { freopen(s.c_str(), "r", stdin); }
	void setO(string s) { freopen(s.c_str(), "w", stdout); }
	void setIO(string s1, string s2) { setI(s1); setO(s2); }
	void setUsacoIO(string s) { setIO(s + ".in", s + ".out"); }
	
	// Sorting lambda syntax
//	auto check = [&](int x, int y) {
//	    return x < y;
//	};
//
//	set<int, decltype(check)> s(check);
//	map<int, int, decltype(check)> mp(check);
//	priority_queue<int, vi, decltype(check)> pq(check); // remember pq.top() is the largest by default (descending order)
}
using namespace cp;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
	
    int n, k;
    cin >> n >> k;

    int h[n];
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }

    int dp[n]; // cost to get to ith stone
    fill(dp, dp + n, INT_MAX);
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= k; j++) {
            if (i + j < n) {
                dp[i+j] = min(dp[i+j], dp[i] + abs(h[i] - h[i+j]));
            }
        }
    }

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

    return 0;
}

Measured memory_bytes: 4424