import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


}
/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