import java.util.*;

class Main {
    public static long solve(int arr[], int n, int k) {
        long ans[] = new long[n + 1];

        ans[1] = 0;
        ans[2] = Math.abs(arr[1] - arr[2]);

        for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        }
        return ans[n];
    }

    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 + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr, n, k));
    }
} import java.util.*; util.* java class Main {
    public static long solve(int arr[], int n, int k) {
        long ans[] = new long[n + 1];

        ans[1] = 0;
        ans[2] = Math.abs(arr[1] - arr[2]);

        for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        }
        return ans[n];
    }

    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 + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr, n, k));
    }
} Main public static long solve(int arr[], int n, int k) {
        long ans[] = new long[n + 1];

        ans[1] = 0;
        ans[2] = Math.abs(arr[1] - arr[2]);

        for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        }
        return ans[n];
    } solve {
        long ans[] = new long[n + 1];

        ans[1] = 0;
        ans[2] = Math.abs(arr[1] - arr[2]);

        for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        }
        return ans[n];
    } long ans[] = new long[n + 1]; ans new long[n + 1] n + 1 n 1 ans[1] = 0 ans[1] ans 1 0 ans[2] = Math.abs(arr[1] - arr[2]) ans[2] ans 2 Math.abs(arr[1] - arr[2]) Math.abs Math arr[1] - arr[2] arr[1] arr 1 arr[2] arr 2 for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        } int i = 3; int i = 3; i 3 i <= n i n i++ i++ i {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        } {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        } ans[i] = Integer.MAX_VALUE ans[i] ans i Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            } int j = 1; int j = 1; j 1 j <= k && (i - j) >= 1 j <= k j k (i - j) >= 1 (i - j) i j 1 j++ j++ j {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            } {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            } ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]) ans[i] ans i Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]) Math.min Math ans[i] ans i Math.abs(arr[i] - arr[i - j]) + ans[i - j] Math.abs(arr[i] - arr[i - j]) Math.abs Math arr[i] - arr[i - j] arr[i] arr i arr[i - j] arr i - j i j ans[i - j] ans i - j i j return ans[n]; ans[n] ans n int arr[] arr int n n int k k 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 + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr, n, k));
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();

        int arr[] = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr, n, k));
    } 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 + 1]; arr new int[n + 1] n + 1 n 1 for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        } int i = 1; int i = 1; i 1 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(solve(arr, n, k)) System.out.println System.out System System.out solve(arr, n, k) solve arr n k String[] args args
import java.util.*;

class Main {
    public static long solve(int arr[], int n, int k) {
        long ans[] = new long[n + 1];

        ans[1] = 0;
        ans[2] = Math.abs(arr[1] - arr[2]);

        for (int i = 3; i <= n; i++) {
            ans[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && (i - j) >= 1; j++) {
                ans[i] = Math.min(ans[i], Math.abs(arr[i] - arr[i - j]) + ans[i - j]);
            }
            // ans[i] = Math.min(Math.abs(arr[i] - arr[i - 1]) + ans[i - 1], Math.abs(arr[i]
            // - arr[i - 2]) + ans[i - 2]);
        }
        return ans[n];
    }

    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 + 1];
        for (int i = 1; i <= n; i++) {
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr, n, k));
    }
}