import java.util.Scanner;

public class Main {

    public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    }
} Main public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    } ans {
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    } int storage[] = new int[n+1]; storage new int[n+1] n+1 n 1 storage[1] = 0 storage[1] storage 1 0 for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        } int i=2; int i=2; i 2 i<=n i n i++ i++ i {
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        } {
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        } storage[i] = Integer.MAX_VALUE storage[i] storage i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           } int j=i-1; int j=i-1; j i-1 i 1 j>=1 && (i-j)<=k j>=1 j 1 (i-j)<=k (i-j) i j k j-- j-- j {
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           } {
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           } storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]) storage[i] storage i Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]) Math.min Math Math.abs(stones[i] - stones[j]) + storage[j] Math.abs(stones[i] - stones[j]) Math.abs Math stones[i] - stones[j] stones[i] stones i stones[j] stones j storage[j] storage j storage[i] storage i return storage[n]; storage[n] storage n int stones[] stones 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 stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, 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 stones[] = new int[n+1]; stones new int[n+1] n+1 n 1 for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        } int i=1; int i=1; i 1 i<=n i n i++ i++ i {
            stones[i] = sc.nextInt();
        } {
            stones[i] = sc.nextInt();
        } stones[i] = sc.nextInt() stones[i] stones i sc.nextInt() sc.nextInt sc System.out.println(ans(stones, n, k)) System.out.println System.out System System.out ans(stones, n, k) ans stones n k String args[] args public class Main {

    public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    }
} public class Main {

    public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    }
} Main
import java.util.Scanner;

public class Main {

    public static int ans(int stones[], int n, int k){
        int storage[] = new int[n+1];
        storage[1] = 0;
        for(int i=2; i<=n; i++){
           storage[i] = Integer.MAX_VALUE;

           for(int j=i-1; j>=1 && (i-j)<=k; j--){
               storage[i] = Math.min(Math.abs(stones[i] - stones[j]) + storage[j], storage[i]);
           }

        }
        return storage[n];
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int stones[] = new int[n+1];
        for(int i=1; i<=n; i++){
            stones[i] = sc.nextInt();
        }
        System.out.println(ans(stones, n, k));
    }
}