import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    }
} Main public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    } main {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    } Scanner ob=new Scanner(System.in); ob new Scanner(System.in) int n=ob.nextInt(); n ob.nextInt() ob.nextInt ob int k=ob.nextInt(); k ob.nextInt() ob.nextInt ob int a[]=new int[n]; a new int[n] n for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            a[i]=ob.nextInt();
        } {
            a[i]=ob.nextInt();
        } a[i]=ob.nextInt() a[i] a i ob.nextInt() ob.nextInt ob int min=0; min 0 int arr[]=new int[n]; arr new int[n] n arr[0]=0 arr[0] arr 0 0 arr[1]=Math.abs(a[1]-a[0]) arr[1] arr 1 Math.abs(a[1]-a[0]) Math.abs Math a[1]-a[0] a[1] a 1 a[0] a 0 for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        } int i=2; int i=2; i 2 i<a.length i a.length a a.length i++ i++ i {
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        } {
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        } min=Integer.MAX_VALUE min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            } int j=1; int j=1; j 1 j<=k j k j++ j++ j {
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            } {
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            } if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                } i-j>=0 i-j i j 0 {
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                } min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min) min Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min) Math.min Math arr[i-j]+Math.abs(a[i]-a[i-j]) arr[i-j] arr 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 min arr[i]=min arr[i] arr i min System.out.println(arr[n-1]) System.out.println System.out System System.out arr[n-1] arr n-1 n 1 ob.close() ob.close ob String[] args args public class Main {
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    }
} public class Main {
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner ob=new Scanner(System.in);
        int n=ob.nextInt();
        int k=ob.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++){
            a[i]=ob.nextInt();
        }
        int min=0;
        int arr[]=new int[n];
        arr[0]=0;
        arr[1]=Math.abs(a[1]-a[0]);
        for(int i=2;i<a.length;i++){
            min=Integer.MAX_VALUE;
            for(int j=1;j<=k;j++){
                if(i-j>=0){
                    min=Math.min(arr[i-j]+Math.abs(a[i]-a[i-j]),min);
                    //System.out.println(min+" "+i+" "+j);
                }
            }
            arr[i]=min;
        }
        System.out.println(arr[n-1]);
        ob.close();
    }
}