import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int k = scanner.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(frog2(arr, k));

        scanner.close();

    }

    public static int frog2(int arr[], int k) {
        int sol[] = new int[arr.length];
        sol[0] = 0;
        for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        }
        return sol[0];

    }

   

} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int k = scanner.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(frog2(arr, k));

        scanner.close();

    }

    public static int frog2(int arr[], int k) {
        int sol[] = new int[arr.length];
        sol[0] = 0;
        for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        }
        return sol[0];

    }

   

} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int k = scanner.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(frog2(arr, k));

        scanner.close();

    } main {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int k = scanner.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(frog2(arr, k));

        scanner.close();

    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int size = scanner.nextInt(); size scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int arr[] = new int[size]; arr new int[size] size for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < size i size i++ i++ i {
            arr[i] = scanner.nextInt();
        } {
            arr[i] = scanner.nextInt();
        } arr[i] = scanner.nextInt() arr[i] arr i scanner.nextInt() scanner.nextInt scanner System.out.println(frog2(arr, k)) System.out.println System.out System System.out frog2(arr, k) frog2 arr k scanner.close() scanner.close scanner String[] args args public static int frog2(int arr[], int k) {
        int sol[] = new int[arr.length];
        sol[0] = 0;
        for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        }
        return sol[0];

    } frog2 {
        int sol[] = new int[arr.length];
        sol[0] = 0;
        for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        }
        return sol[0];

    } int sol[] = new int[arr.length]; sol new int[arr.length] arr.length arr arr.length sol[0] = 0 sol[0] sol 0 0 for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        } int i = arr.length - 2; int i = arr.length - 2; i arr.length - 2 arr.length arr arr.length 2 i >= 0 i 0 i-- i-- i {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        } {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        } int cost = Integer.MAX_VALUE; cost Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            } {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            } if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                } i + j < arr.length i + j i j arr.length arr arr.length {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost) cost Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost) Math.min Math Math.abs(arr[i + j] - arr[i]) + sol[i + j] Math.abs(arr[i + j] - arr[i]) Math.abs Math arr[i + j] - arr[i] arr[i + j] arr i + j i j arr[i] arr i sol[i + j] sol i + j i j cost {
                    break;
                } break; sol[i] = cost sol[i] sol i cost return sol[0]; sol[0] sol 0 int arr[] arr int k k
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int k = scanner.nextInt();
        int arr[] = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(frog2(arr, k));

        scanner.close();

    }

    public static int frog2(int arr[], int k) {
        int sol[] = new int[arr.length];
        sol[0] = 0;
        for (int i = arr.length - 2; i >= 0; i--) {
            int cost = Integer.MAX_VALUE;
            for (int j = 1; j <= k; j++) {
                if (i + j < arr.length) {
                    cost = Math.min(Math.abs(arr[i + j] - arr[i]) + sol[i + j], cost);

                } else {
                    break;
                }
            }
            sol[i] = cost;
        }
        return sol[0];

    }

   

}