import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static int minCost(int[] h, int i, int[] dp, int k) {
        if (i == 0) {
            return 0;
        }

        int cost = Integer.MAX_VALUE;
        if (dp[i] == -1) {
            for (int j = 1; j <= k; j++) {
                if (i - j >= 0) {
                    cost = Integer.min(cost, minCost(h, i - j, dp, k) + Math.abs(h[i] - h[i - j]));
                }
            }
            dp[i] = cost;
        }
        return dp[i];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try {
            int size = scanner.nextInt();
            int k = scanner.nextInt();
            int[] h = new int[size];
            for (int i = 0; i < h.length; i++) {
                h[i] = scanner.nextInt();
            }
            int[] dp = new int[size];
            Arrays.fill(dp, -1);
            System.out.println(minCost(h, h.length - 1, dp, k));

        } finally {
            scanner.close();
        }
    }
}
/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