import java.io.*;
import java.util.*;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    }

    public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    }

} import java.io.*; io.* java import java.util.*; util.* java public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    }

    public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    }

} Main /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    } main {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int[] h = new int[n]; h new int[n] n for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            h[i] = scan.nextInt();
        } {
            h[i] = scan.nextInt();
        } h[i] = scan.nextInt() h[i] h i scan.nextInt() scan.nextInt scan int solve = dp(n, h, k); solve dp(n, h, k) dp n h k System.out.println(solve) System.out.println System.out System System.out solve String[] args args public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    } dp {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    } int[] arr = new int[n]; arr new int[n] n arr[1] = Math.abs(h[0] - h[1]) arr[1] arr 1 Math.abs(h[0] - h[1]) Math.abs Math h[0] - h[1] h[0] h 0 h[1] h 1 for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        } int i = 2; int i = 2; i 2 i < arr.length i arr.length arr arr.length i++ i++ i {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        } {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            } {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            } if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            } i-k>=0 i-k i k 0 {
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            } min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]) min Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]) Math.min Math min Math.abs(h[i-k]-h[i])+arr[i-k] Math.abs(h[i-k]-h[i]) Math.abs Math h[i-k]-h[i] h[i-k] h i-k i k h[i] h i arr[i-k] arr i-k i k arr[i] = min arr[i] arr i min return arr[n - 1]; arr[n - 1] arr n - 1 n 1 int n n int[] h h int K K public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    }

    public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    }

} public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    }

    public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    }

} Main
import java.io.*;
import java.util.*;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = scan.nextInt();
        }
        int solve = dp(n, h, k);
        System.out.println(solve);
    }

    public static int dp(int n, int[] h, int K) {
        int[] arr = new int[n];
        arr[1] = Math.abs(h[0] - h[1]);
        for (int i = 2; i < arr.length; i++) {
            int min = Integer.MAX_VALUE;
            for (int k = 1; k <= K; k++) {
                if(i-k>=0){
                min  = Math.min(min, Math.abs(h[i-k]-h[i])+arr[i-k]);
            }
                arr[i] = min;
            }
        }
        return arr[n - 1];
    }

}