//#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define lowbit(x) (x&(-x))
const int inf = 0x3f3f3f3f;
typedef long long ll;
const int N = 1e5 + 10;
typedef pair<int, int> P;
const int up = 20;
const int mod = 1e9 + 7;
int dp[N],n,k,a[N];
int main()
{
	scanf("%d%d", &n,&k);
	for (int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		dp[i] = inf;
	}
	dp[1] = 0;
	for (int i = 2; i <= n; i++){
		for (int j = 1; j <= k; j++){
			if (i - j >= 1) dp[i] = min(dp[i], dp[i-j] + abs(a[i] - a[i-j]));
			else break;
		}
	}
	cout << dp[n] << endl;
	return 0;
}