import java.util.*;

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

class Main{
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] arr = new int[n];
        for(int i=0;i<n;i++)arr[i] = scan.nextInt();
        scan.close();
        int[] dp = new int[n+1];
        Arrays.fill(dp,1000000005);
        dp[0] = 0;
        for(int i= 0;i<n;i++){
            for(int j=1;j<=k;j++){
                if(i+j < n)dp[i+j] = Math.min(dp[i+j],dp[i] + Math.abs(arr[i]- arr[i+j]));
            }
        }
        System.out.println(dp[n-1]);
        }
}
