import java.util.*;
import java.io.*;
public class Main {
    public static int solve(int n , int k , int arr[]){

        int dp[] = new int[n];

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

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

        int dp[] = new int[n];

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

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

        int dp[] = new int[n];

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

        return dp[n-1];
    } solve {

        int dp[] = new int[n];

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

        return dp[n-1];
    } int dp[] = new int[n]; dp new int[n] n for(int i = 1 ; i < n ; i++){
            int ans = Integer.MAX_VALUE;
            for(int j = i-1 ; j >= 0 && (i-j) <= k ; j--){
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            }
            dp[i] = ans;
        } int i = 1 ; int i = 1 ; i 1 i < n i n i++ i++ i {
            int ans = Integer.MAX_VALUE;
            for(int j = i-1 ; j >= 0 && (i-j) <= k ; j--){
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            }
            dp[i] = ans;
        } {
            int ans = Integer.MAX_VALUE;
            for(int j = i-1 ; j >= 0 && (i-j) <= k ; j--){
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            }
            dp[i] = ans;
        } int ans = Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = i-1 ; j >= 0 && (i-j) <= k ; j--){
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            } int j = i-1 ; int j = i-1 ; j i-1 i 1 j >= 0 && (i-j) <= k j >= 0 j 0 (i-j) <= k (i-j) i j k j-- j-- j {
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            } {
                int diff = Math.abs(arr[i] - arr[j]);
                ans = Math.min(ans , diff + dp[j]);
            } int diff = Math.abs(arr[i] - arr[j]); diff Math.abs(arr[i] - arr[j]) Math.abs Math arr[i] - arr[j] arr[i] arr i arr[j] arr j ans = Math.min(ans , diff + dp[j]) ans Math.min(ans , diff + dp[j]) Math.min Math ans diff + dp[j] diff dp[j] dp j dp[i] = ans dp[i] dp i ans return dp[n-1]; dp[n-1] dp n-1 n 1 int n n int k k int arr[] arr public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int arr[] = new int[n];
        for(int i = 0 ; i < n ; i++){
            arr[i] = s.nextInt();
        }
        System.out.println(solve(n , k , arr));
    } main {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int arr[] = new int[n];
        for(int i = 0 ; i < n ; i++){
            arr[i] = s.nextInt();
        }
        System.out.println(solve(n , k , arr));
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int n = s.nextInt(); n s.nextInt() s.nextInt s int k = s.nextInt(); k s.nextInt() s.nextInt s int arr[] = new int[n]; arr new int[n] n for(int i = 0 ; i < n ; i++){
            arr[i] = s.nextInt();
        } int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
            arr[i] = s.nextInt();
        } {
            arr[i] = s.nextInt();
        } arr[i] = s.nextInt() arr[i] arr i s.nextInt() s.nextInt s System.out.println(solve(n , k , arr)) System.out.println System.out System System.out solve(n , k , arr) solve n k arr String args[] args public class Main {
    public static int solve(int n , int k , int arr[]){

        int dp[] = new int[n];

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

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

        int dp[] = new int[n];

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

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

        int dp[] = new int[n];

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

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