import java.util.Scanner;

// https://atcoder.jp/contests/dp/tasks/dp_b
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        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);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    }
} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    } main {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) String[] line = scanner.nextLine().split(" "); line scanner.nextLine().split(" ") scanner.nextLine().split scanner.nextLine() scanner.nextLine scanner " " int n = Integer.parseInt(line[0]); n Integer.parseInt(line[0]) Integer.parseInt Integer line[0] line 0 int k = Integer.parseInt(line[1]); k Integer.parseInt(line[1]) Integer.parseInt Integer line[1] line 1 int[] nums = new int[n]; nums new int[n] n int[] dp = new int[n]; dp new int[n] n line = scanner.nextLine().split(" ") line scanner.nextLine().split(" ") scanner.nextLine().split scanner.nextLine() scanner.nextLine scanner " " for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        } {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        } nums[i] = Integer.parseInt(line[i]) nums[i] nums i Integer.parseInt(line[i]) Integer.parseInt Integer line[i] line i dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        } {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        } int maxLen = Math.min(i + k, nums.length - 1); maxLen Math.min(i + k, nums.length - 1) Math.min Math i + k i k nums.length - 1 nums.length nums nums.length 1 for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            } int j = i; int j = i; j i j <= maxLen j maxLen j++ j++ j {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            } {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            } dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i])) dp[j] dp j Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i])) Math.min Math dp[j] dp j dp[i] + Math.abs(nums[j] - nums[i]) dp[i] dp i Math.abs(nums[j] - nums[i]) Math.abs Math nums[j] - nums[i] nums[j] nums j nums[i] nums i 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);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    }
} Main
import java.util.Scanner;

// https://atcoder.jp/contests/dp/tasks/dp_b
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] line = scanner.nextLine().split(" ");
        int n = Integer.parseInt(line[0]);
        int k = Integer.parseInt(line[1]);

        int[] nums = new int[n];
        int[] dp = new int[n];
        line = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(line[i]);
            dp[i] = Integer.MAX_VALUE;
        }

        dp[0] = 0;

        for (int i = 0; i < n; i++) {
            int maxLen = Math.min(i + k, nums.length - 1);

            for (int j = i; j <= maxLen; j++) {
                dp[j] = Math.min(dp[j], dp[i] + Math.abs(nums[j] - nums[i]));
            }
        }
        System.out.println(dp[n - 1]);
    }
}