#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<map>
#include<bitset>
#include<utility>
//#include<unordered_map>
#ifdef LOCAL
#include"file.h"
#endif

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
const double eps = 1e-12;
const double Pi = 3.1415926535897932;
const int dir[4][2] = { -1,0,1,0,0,-1,0,1 };
//const int dir[8][2] = { -2,-1,-2,1,2,-1,2,1,-1,2,-1,-2,1,2,1,-2 };








const int maxn = 2e5 + 5;




int f[maxn];
int h[maxn];

int n, k;

int main() {
#ifdef LOCAL
	fileopen();
#endif

	while (scanf("%d%d", &n, &k) == 2) {
		for (int i = 1; i <= n; i++)
			scanf("%d", &h[i]);

		memset(f, 63, sizeof(f));
		f[1] = 0;

		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= k; j++) {
				f[i + j] = min(f[i + j], f[i] + abs(h[i] - h[i + j]));
			}
		}
		printf("%d\n", f[n]);
	}


#ifdef LOCAL
	fileclose();
#endif
	return 0;
}
