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[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    }

    public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return 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[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    }

    public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return 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[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    } main {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    } 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[] height = new int[n]; height new int[n] n for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            height[i] = scanner.nextInt();
        } {
            height[i] = scanner.nextInt();
        } height[i] = scanner.nextInt() height[i] height i scanner.nextInt() scanner.nextInt scanner System.out.println(new Main().f(n, k, height)) System.out.println System.out System System.out new Main().f(n, k, height) new Main().f new Main() n k height String[] args args public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return dp[n-1];
    } f {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return dp[n-1];
    } int[] dp = new int[n]; dp new int[n] n if(n <= 1)
            return 0; n <= 1 n 1 return 0; 0 for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        } {
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        } dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            } int j = 1; int j = 1; j 1 j <= k && i - j >= 0 j <= k j k i - j >= 0 i - j i j 0 j++ j++ j {
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            } {
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            } dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j])) dp[i] dp i Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j])) Math.min Math dp[i] dp i dp[i-j] + Math.abs(height[i] - height[i-j]) dp[i-j] dp i-j i j Math.abs(height[i] - height[i-j]) Math.abs Math height[i] - height[i-j] height[i] height i height[i-j] height i-j i j return dp[n-1]; dp[n-1] dp n-1 n 1 int n n int k k int[] height height public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        int[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    }

    public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return 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[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    }

    public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return 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[] height = new int[n];
        for(int i = 0; i < n; i++){
            height[i] = scanner.nextInt();
        }
        System.out.println(new Main().f(n, k, height));
    }

    public int f(int n, int k, int[] height) {
        int[] dp = new int[n];
        if(n <= 1)
            return 0;
        for(int i = 1; i < n; i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 1; j <= k && i - j >= 0; j++){
                dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(height[i] - height[i-j]));
            }
        }
        return dp[n-1];
    }
}
