import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    } main {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int[] arr = new int[n]; arr new int[n] n for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        } int i=0 ; int i=0 ; i 0 i<n i n ++i ++i i {
            arr[i] = scanner.nextInt();
        } {
            arr[i] = scanner.nextInt();
        } arr[i] = scanner.nextInt() arr[i] arr i scanner.nextInt() scanner.nextInt scanner int[] dp = new int[n]; dp new int[n] n for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        } int i = 1 ; int i = 1 ; i 1 i<=k&&i<n i<=k i k i<n i n ++i ++i i {
            dp[i] = Math.abs(arr[i]-arr[0]);
        } {
            dp[i] = Math.abs(arr[i]-arr[0]);
        } dp[i] = Math.abs(arr[i]-arr[0]) dp[i] dp i Math.abs(arr[i]-arr[0]) Math.abs Math arr[i]-arr[0] arr[i] arr i arr[0] arr 0 for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        } int i=k+1 ; int i=k+1 ; i k+1 k 1 i<n i n ++i ++i i {
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        } {
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        } int minValue = Integer.MAX_VALUE; minValue Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            } int j = k ; int j = k ; j k j>0 j 0 --j --j j {
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            } {
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            } int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]); temp dp[i-j]+Math.abs(arr[i]-arr[i-j]) dp[i-j] dp i-j i j Math.abs(arr[i]-arr[i-j]) Math.abs Math arr[i]-arr[i-j] arr[i] arr i arr[i-j] arr i-j i j if(temp<minValue){
                    minValue = temp;
                } temp<minValue temp minValue {
                    minValue = temp;
                } minValue = temp minValue temp dp[i] = minValue dp[i] dp i minValue 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 public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] arr = new int[n];
        for(int i=0 ; i<n ;++i){
            arr[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        for(int i = 1 ; i<=k&&i<n;++i){
            dp[i] = Math.abs(arr[i]-arr[0]);
        }
        for(int i=k+1 ; i<n;++i){
            int minValue = Integer.MAX_VALUE;
            for(int j = k ; j>0 ; --j){
                int temp =dp[i-j]+Math.abs(arr[i]-arr[i-j]);
                if(temp<minValue){
                    minValue = temp;
                }
            }
            dp[i] = minValue;
        }
        System.out.println(dp[n-1]);
    }
}
