
import java.util.Scanner;


public class Main {

	static int k;
	static long n;
	static long[] stones;
	static long memo[];

	public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	}

	public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	}

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

	static int k;
	static long n;
	static long[] stones;
	static long memo[];

	public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	}

	public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	}

} Main static int k; k static long n; n static long[] stones; stones static long memo[]; memo public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	} main {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	} String input1 = ""; input1 "" String input2 = ""; input2 "" Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) input1 = scanner.nextLine() input1 scanner.nextLine() scanner.nextLine scanner input2 = scanner.nextLine() input2 scanner.nextLine() scanner.nextLine scanner String[] n2 = input1.split(" "); n2 input1.split(" ") input1.split input1 " " n = Long.parseLong(n2[0]) n Long.parseLong(n2[0]) Long.parseLong Long n2[0] n2 0 k = Integer.parseInt(n2[1]) k Integer.parseInt(n2[1]) Integer.parseInt Integer n2[1] n2 1 String[] arr = input2.split(" "); arr input2.split(" ") input2.split input2 " " stones = new long[(int) n] stones new long[(int) n] (int) n for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		} int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
			stones[i] = Integer.parseInt(arr[i]);
		} {
			stones[i] = Integer.parseInt(arr[i]);
		} stones[i] = Integer.parseInt(arr[i]) stones[i] stones i Integer.parseInt(arr[i]) Integer.parseInt Integer arr[i] arr i memo = new long[(int) n] memo new long[(int) n] (int) n for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		} int i = 0; int i = 0; i 0 i < memo.length i memo.length memo memo.length i++ i++ i {
			memo[i] = -1;
		} {
			memo[i] = -1;
		} memo[i] = -1 memo[i] memo i -1 1 System.out.println(getMinCostStone(0)) System.out.println System.out System System.out getMinCostStone(0) getMinCostStone 0 scanner.close() scanner.close scanner String[] args args public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	} getMinCostStone {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	} if (i == stones.length - 1) {
			return 0;
		} i == stones.length - 1 i stones.length - 1 stones.length stones stones.length 1 {
			return 0;
		} return 0; 0 long cost = memo[i]; cost memo[i] memo i if (cost != -1) {
			return cost;
		} cost != -1 cost -1 1 {
			return cost;
		} return cost; cost cost = (long) 1e18 cost (long) 1e18 memo[i] = cost memo[i] memo i cost if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		} i != stones.length - k i stones.length - k stones.length stones stones.length k {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		} for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			} {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			} if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				} i + j < stones.length i + j i j stones.length stones stones.length {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				} cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j])) cost Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j])) Long.min Long cost getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]) getMinCostStone(i + j) getMinCostStone i + j i j Math.abs(stones[i] - stones[i + j]) Math.abs Math stones[i] - stones[i + j] stones[i] stones i stones[i + j] stones i + j i j memo[i] = cost memo[i] memo i cost return memo[i] = cost; memo[i] = cost memo[i] memo i cost int i i public class Main {

	static int k;
	static long n;
	static long[] stones;
	static long memo[];

	public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	}

	public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	}

} public class Main {

	static int k;
	static long n;
	static long[] stones;
	static long memo[];

	public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	}

	public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	}

} Main

import java.util.Scanner;


public class Main {

	static int k;
	static long n;
	static long[] stones;
	static long memo[];

	public static void main(String[] args) {

		String input1 = "";
		String input2 = "";
		Scanner scanner = new Scanner(System.in);
		input1 = scanner.nextLine();
		input2 = scanner.nextLine();
		String[] n2 = input1.split(" ");

		n = Long.parseLong(n2[0]);
		k = Integer.parseInt(n2[1]);
		String[] arr = input2.split(" ");
		stones = new long[(int) n];
		for (int i = 0; i < arr.length; i++) {
			stones[i] = Integer.parseInt(arr[i]);
		}

		memo = new long[(int) n];
		for (int i = 0; i < memo.length; i++) {
			memo[i] = -1;
		}
		System.out.println(getMinCostStone(0));
		scanner.close();
	}

	public static long getMinCostStone(int i) {
		if (i == stones.length - 1) {
			return 0;
		}
		long cost = memo[i];

		if (cost != -1) {
			return cost;
		}
		cost = (long) 1e18;
		memo[i] = cost;
		if (i != stones.length - k) {
			for (int j = 1; j <= k; j++) {
				if (i + j < stones.length) {
					cost = Long.min(cost, getMinCostStone(i + j) + Math.abs(stones[i] - stones[i + j]));
					memo[i] = cost;
				}
			}
		}
		return memo[i] = cost;
	}

}
