#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int h[(int)1e5], dp[(int)1e5], K, N;
int main()
{
	K = N = 0;
	for(int j = 1;j <= 1e5;j++)
	{
		dp[j] = 1e9;
	}
	scanf("%d %d", &N, &K);
	for(int i = 1;i <= N;i++)
	{
		scanf("%d", &h[i]);		
	}
	dp[1] = 0;
	dp[2] = dp[1] + abs(h[1] - h[2]);
	for(int i = 1;i <= N;i++)
	{
		for(int j = 1;j <= K;j++)		
		{
			dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
		}
	}
	printf("%d\n", dp[N]);
	return 0;	
}
