import java.util.*;

public class Main {
    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();
        }

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

        System.out.println(dp[n]);

    }
} import java.util.*; util.* java public class Main {
    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();
        }

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

        System.out.println(dp[n]);

    }
} Main 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();
        }

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

        System.out.println(dp[n]);

    } 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();
        }

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

        System.out.println(dp[n]);

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

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

        System.out.println(dp[n]);

    }
} public class Main {
    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();
        }

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

        System.out.println(dp[n]);

    }
} Main
import java.util.*;

public class Main {
    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();
        }

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

        System.out.println(dp[n]);

    }
}