//package atcoderdp;
import 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 a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(find(a,n,k));
    }
    public static int find(int a[],int n,int k){
        int dp[] = new int [n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(a[0]-a[1]);
        for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        }
      //  System.out.println(Arrays.toString(dp));
        return dp[n-1];
    }
} import java.util.*; util.* java class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(find(a,n,k));
    }
    public static int find(int a[],int n,int k){
        int dp[] = new int [n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(a[0]-a[1]);
        for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        }
      //  System.out.println(Arrays.toString(dp));
        return 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 a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(find(a,n,k));
    } main {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int k = s.nextInt();
        int a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(find(a,n,k));
    } 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 a[] = new int[n]; a new int[n] n for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        } int i = 0; int i = 0; i 0 i <n i n i++ i++ i {
            a[i] = s.nextInt();
        } {
            a[i] = s.nextInt();
        } a[i] = s.nextInt() a[i] a i s.nextInt() s.nextInt s System.out.println(find(a,n,k)) System.out.println System.out System System.out find(a,n,k) find a n k String[] args args public static int find(int a[],int n,int k){
        int dp[] = new int [n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(a[0]-a[1]);
        for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        }
      //  System.out.println(Arrays.toString(dp));
        return dp[n-1];
    } find {
        int dp[] = new int [n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(a[0]-a[1]);
        for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        }
      //  System.out.println(Arrays.toString(dp));
        return dp[n-1];
    } int dp[] = new int [n]; dp new int [n] n Arrays.fill(dp,Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0]=0 dp[0] dp 0 0 dp[1]=Math.abs(a[0]-a[1]) dp[1] dp 1 Math.abs(a[0]-a[1]) Math.abs Math a[0]-a[1] a[0] a 0 a[1] a 1 for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        } int i=2; int i=2; i 2 i<n i n i++ i++ i {
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        } {
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            } int j=1; int j=1; j 1 j<=Math.min(k,i) j Math.min(k,i) Math.min Math k i j++ j++ j {
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            } {
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            } min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j])) min Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j])) Math.min Math min dp[i-j]+Math.abs(a[i]-a[i-j]) dp[i-j] dp i-j i j Math.abs(a[i]-a[i-j]) Math.abs Math a[i]-a[i-j] a[i] a i a[i-j] a i-j i j dp[i] = min dp[i] dp i min return dp[n-1]; dp[n-1] dp n-1 n 1 int a[] a int n n int k k
//package atcoderdp;
import 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 a[] = new int[n];
        for (int i = 0; i <n ; i++) {
            a[i] = s.nextInt();
        }
        System.out.println(find(a,n,k));
    }
    public static int find(int a[],int n,int k){
        int dp[] = new int [n];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        dp[1]=Math.abs(a[0]-a[1]);
        for(int i=2;i<n;i++){
            int min = Integer.MAX_VALUE;
            for(int j=1;j<=Math.min(k,i);j++){
                min = Math.min(min,dp[i-j]+Math.abs(a[i]-a[i-j]));
            }
            dp[i] = min;
        }
      //  System.out.println(Arrays.toString(dp));
        return dp[n-1];
    }
}
