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

public class Main {
    public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
    public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    }
} Main public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    } dpSolution {
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    } int dp[] = new int[n+1]; dp new int[n+1] n+1 n 1 Arrays.fill(dp,-1) Arrays.fill Arrays dp -1 1 dp[0] = 0 dp[0] dp 0 0 dp[1] = 0 dp[1] dp 1 0 dp[2] = Math.abs(a[1]-a[2]) dp[2] dp 2 Math.abs(a[1]-a[2]) Math.abs Math a[1]-a[2] a[1] a 1 a[2] a 2 for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        } int i=3; int i=3; i 3 i<=n i n i++ i++ i {
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        } {
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        } int j= i-k; j i-k i k int val = Integer.MAX_VALUE; val Integer.MAX_VALUE Integer Integer.MAX_VALUE if(j<=0) {
                j = 1;
            } j<=0 j 0 {
                j = 1;
            } j = 1 j 1 for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            } ; j<i j i j++ j++ j {
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            } {
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            } val = Math.min(val,dp[j]+Math.abs(a[i]-a[j])) val Math.min(val,dp[j]+Math.abs(a[i]-a[j])) Math.min Math val dp[j]+Math.abs(a[i]-a[j]) dp[j] dp j Math.abs(a[i]-a[j]) Math.abs Math a[i]-a[j] a[i] a i a[j] a j dp[i] = val dp[i] dp i val return dp[n]; dp[n] dp n int n n int k k int a[] a public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    } 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 a[] = new int[n+1]; a new int[n+1] n+1 n 1 for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        } int i=1; int i=1; i 1 i<=n i n i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc System.out.println(dpSolution(n,k,a)) System.out.println System.out System System.out dpSolution(n,k,a) dpSolution n k a String args[] args public class Main {
    public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    }
} public class Main {
    public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    }
} Main

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

public class Main {
    public static int dpSolution(int n,int k,int a[]){
       int dp[] = new int[n+1];
        Arrays.fill(dp,-1);
        dp[0] = 0;
        dp[1] = 0;
        dp[2] = Math.abs(a[1]-a[2]);
        for(int i=3;i<=n;i++){
            int j= i-k;
            int val = Integer.MAX_VALUE;
            if(j<=0) {
                j = 1;
            }
            for(;j<i;j++){
                 val = Math.min(val,dp[j]+Math.abs(a[i]-a[j]));
                 //System.out.println(i+" "+j+" "+dp[j]+" "+val);

            }
            dp[i] = val;
        }
        return dp[n];
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int a[] = new int[n+1];
        for(int i=1;i<=n;i++){
            a[i] = sc.nextInt();
        }

        System.out.println(dpSolution(n,k,a));

    }
}
