import java.util.*;
import java.lang.*;
import java.io.*;


public class Main
{   
    static int h[];
    static int dp[];
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scan = new Scanner(System.in);
		int sz = scan.nextInt();
		int k = scan.nextInt();
		h = new int[sz];
		dp = new int[sz];
		for(int i = 0; i < sz; i++) h[i] = scan.nextInt();
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 2; i < sz; i++){
		    int min = Integer.MAX_VALUE;
		    for(int j = 1; i - j >= 0 && j <= k; j++)
		    min = Math.min(dp[i - j] + Math.abs(h[i] - h[i - j]), min);
		    dp[i] = min;
		}
		System.out.println(dp[sz - 1]);
	}
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz