import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(execute(arr, n, k));
        sc.close();
    }

    private static int execute(int[] arr, int n, int k) {

        int[] dp = new int[n];
        dp[0] = 0;

        dp[1] = Math.abs(arr[1] - arr[0]);
        for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        }
        return dp[n - 1];
    }
} import java.util.Scanner; Scanner java.util class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(execute(arr, n, k));
        sc.close();
    }

    private static int execute(int[] arr, int n, int k) {

        int[] dp = new int[n];
        dp[0] = 0;

        dp[1] = Math.abs(arr[1] - arr[0]);
        for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        }
        return dp[n - 1];
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(execute(arr, n, k));
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(execute(arr, n, k));
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc System.out.println(execute(arr, n, k)) System.out.println System.out System System.out execute(arr, n, k) execute arr n k sc.close() sc.close sc String[] args args private static int execute(int[] arr, int n, int k) {

        int[] dp = new int[n];
        dp[0] = 0;

        dp[1] = Math.abs(arr[1] - arr[0]);
        for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        }
        return dp[n - 1];
    } execute {

        int[] dp = new int[n];
        dp[0] = 0;

        dp[1] = Math.abs(arr[1] - arr[0]);
        for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        }
        return dp[n - 1];
    } int[] dp = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(arr[1] - arr[0]) dp[1] dp 1 Math.abs(arr[1] - arr[0]) Math.abs Math arr[1] - arr[0] arr[1] arr 1 arr[0] arr 0 for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        } {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            } int j = 0; int j = 0; j 0 j < k j k j++ j++ j {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            } {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            } if (i - 1 - j < 0) break; i - 1 - j < 0 i - 1 - j i - 1 - j i 1 j 0 break; int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]); val dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]) dp[i - 1 - j] dp i - 1 - j i - 1 - j i 1 j Math.abs(arr[i] - arr[i - 1 - j]) Math.abs Math arr[i] - arr[i - 1 - j] arr[i] arr i arr[i - 1 - j] arr i - 1 - j i - 1 - j i 1 j if (val < min) {
                    min = val;
                } val < min val min {
                    min = val;
                } min = val min val dp[i] = min dp[i] dp i min return dp[n - 1]; dp[n - 1] dp n - 1 n 1 int[] arr arr int n n int k k
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        System.out.println(execute(arr, n, k));
        sc.close();
    }

    private static int execute(int[] arr, int n, int k) {

        int[] dp = new int[n];
        dp[0] = 0;

        dp[1] = Math.abs(arr[1] - arr[0]);
        for (int i = 2; i < n; i++) {

            int min = Integer.MAX_VALUE;
            for (int j = 0; j < k; j++) {
                if (i - 1 - j < 0) break;
                ;
                int val = dp[i - 1 - j] + Math.abs(arr[i] - arr[i - 1 - j]);
                if (val < min) {
                    min = val;
                }
            }

            dp[i] = min;

        }
        return dp[n - 1];
    }
}
