#include <bits/stdc++.h>

using namespace std;

int n,K;
int h[100010],f[100010];

int main(){
	scanf("%d%d",&n,&K);
	for(int i=1;i<=n;i++){
		scanf("%d",h+i);
		f[i]=i==1?0:1e9;
		
		for(int j=1;j<=K&&i-j>0;j++)
			f[i]=min(f[i],f[i-j]+abs(h[i]-h[i-j]));
	}

	printf("%d\n",f[n]);

	return 0;
}