import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


} import java.util.Scanner; Scanner java.util public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


} Main public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    } main {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int i = scanner.nextInt(); i scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int heights[] = new int[i]; heights new int[i] i for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        } int j = 0; int j = 0; j 0 j < heights.length j heights.length heights heights.length j++ j++ j {
            heights[j] = scanner.nextInt();
        } {
            heights[j] = scanner.nextInt();
        } heights[j] = scanner.nextInt() heights[j] heights j scanner.nextInt() scanner.nextInt scanner long numOfWays = getNumOfWays(heights, k); numOfWays getNumOfWays(heights, k) getNumOfWays heights k System.out.println(numOfWays) System.out.println System.out System System.out numOfWays String[] args args private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    } getNumOfWays {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    } long dp[] = new long[hei.length]; dp new long[hei.length] hei.length hei hei.length for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        } int i = dp.length - 2; int i = dp.length - 2; i dp.length - 2 dp.length dp dp.length 2 i >= 0 i 0 i-- i-- i {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        } {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        } long cost = Integer.MAX_VALUE; cost Integer.MAX_VALUE Integer Integer.MAX_VALUE int end = Math.min(dp.length - 1, i + k); end Math.min(dp.length - 1, i + k) Math.min Math dp.length - 1 dp.length dp dp.length 1 i + k i k for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            } int j = i + 1; int j = i + 1; j i + 1 i 1 j <= end j end j++ j++ j {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            } {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            } cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost) cost Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost) Math.min Math Math.abs(hei[i] - hei[j]) + dp[j] Math.abs(hei[i] - hei[j]) Math.abs Math hei[i] - hei[j] hei[i] hei i hei[j] hei j dp[j] dp j cost dp[i] = cost dp[i] dp i cost return dp[0]; dp[0] dp 0 int hei[] hei int k k public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


} public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


} Main
import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        // write your code here
        Scanner scanner = new Scanner(System.in);
        int i = scanner.nextInt();
        int k = scanner.nextInt();
        int heights[] = new int[i];
        for (int j = 0; j < heights.length; j++) {
            heights[j] = scanner.nextInt();
        }
        long numOfWays = getNumOfWays(heights, k);
        System.out.println(numOfWays);
    }

    private static long getNumOfWays(int hei[], int k) {

        long dp[] = new long[hei.length];
        for (int i = dp.length - 2; i >= 0; i--) {
            long cost = Integer.MAX_VALUE;
            int end = Math.min(dp.length - 1, i + k);
            for (int j = i + 1; j <= end; j++) {
                cost = Math.min(Math.abs(hei[i] - hei[j]) + dp[j], cost);
            }
            dp[i] = cost;
        }
        return dp[0];
    }


}