import java.util.Arrays;
import java.util.Scanner;

class Main {
    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();
        }
        int dp[] = new int[n];
        dp[0] = 0;
        for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        }
        System.out.println(dp[n-1]);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util class Main {
    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();
        }
        int dp[] = new int[n];
        dp[0] = 0;
        for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        }
        System.out.println(dp[n-1]);
    }
} Main 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();
        }
        int dp[] = new int[n];
        dp[0] = 0;
        for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        }
        System.out.println(dp[n-1]);
    } 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();
        }
        int dp[] = new int[n];
        dp[0] = 0;
        for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        }
        System.out.println(dp[n-1]);
    } 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 int dp[] = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        } int i= 1; int i= 1; i 1 i<n i n i++ i++ i {
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        } {
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        } int min = 1000000007; min 1000000007 for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            } int j = i-1; int j = i-1; j i-1 i 1 j>=0 && j>=(i-k) j>=0 j 0 j>=(i-k) j (i-k) i k j-- j-- j {
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            } {
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            } min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min) min Math.min(Math.abs(arr[i] - arr[j])+dp[j],min) Math.min Math Math.abs(arr[i] - arr[j])+dp[j] Math.abs(arr[i] - arr[j]) Math.abs Math arr[i] - arr[j] arr[i] arr i arr[j] arr j dp[j] dp j min dp[i] = min dp[i] dp i min System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 String[] args args
import java.util.Arrays;
import java.util.Scanner;

class Main {
    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();
        }
        int dp[] = new int[n];
        dp[0] = 0;
        for(int i= 1;i<n;i++){
            int min = 1000000007;
            for(int j = i-1;j>=0 && j>=(i-k);j--){
                min = Math.min(Math.abs(arr[i] - arr[j])+dp[j],min);
            }
            dp[i] = min;
        }
        System.out.println(dp[n-1]);
    }
}
