#include<cstdio>
#include<algorithm>
#include<cstring>
int main()
{
	long long d[100005];
	int a[100005];
	int n,k,i,mi;
	scanf("%d %d",&n ,&k);
	for(i = 1;i <= n;i++)
		scanf("%d",&a[i]);
	mi = n;
	memset(d,0,sizeof(d));
	while(n > k + 1)
	{
		d[n - 1] = d[n] + abs(a[n] - a[n - 1]);
		mi = n - 1;
		for(i = 2;i <= k;i++)
		{
			d[n - i] = d[n] + abs(a[n] - a[n - i]);
			if(d[n - i] <= d[mi])	mi = n - i;
			//printf("%lld\n",d[mi]);
			//printf("%d\n",mi);
		}
		n = mi;
		//printf("%d\n",n);
	}
	d[1] = d[n] + abs(a[n] - a[1]);
	printf("%lld\n",d[1]);
	return 0;
}