
import java.io.BufferedReader;
import java.io.InputStreamReader;

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

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static int minCost(int stones[], int k) {
		int cost[] = new int[stones.length];
		cost[0] = 0;
		cost[1] = Math.abs(stones[1] - stones[0]);
		for (int i = 2; i < stones.length; i++) {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		}
		return cost[stones.length - 1];
	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	}

} Main public static int minCost(int stones[], int k) {
		int cost[] = new int[stones.length];
		cost[0] = 0;
		cost[1] = Math.abs(stones[1] - stones[0]);
		for (int i = 2; i < stones.length; i++) {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		}
		return cost[stones.length - 1];
	} minCost {
		int cost[] = new int[stones.length];
		cost[0] = 0;
		cost[1] = Math.abs(stones[1] - stones[0]);
		for (int i = 2; i < stones.length; i++) {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		}
		return cost[stones.length - 1];
	} int cost[] = new int[stones.length]; cost new int[stones.length] stones.length stones stones.length cost[0] = 0 cost[0] cost 0 0 cost[1] = Math.abs(stones[1] - stones[0]) cost[1] cost 1 Math.abs(stones[1] - stones[0]) Math.abs Math stones[1] - stones[0] stones[1] stones 1 stones[0] stones 0 for (int i = 2; i < stones.length; i++) {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		} int i = 2; int i = 2; i 2 i < stones.length i stones.length stones stones.length i++ i++ i {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		} {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		} cost[i] = Integer.MAX_VALUE cost[i] cost i Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			} {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			} if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				} i - j >= 0 i - j i j 0 {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				} cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]) cost[i] cost i Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]) Math.min Math Math.abs(stones[i - j] - stones[i]) + cost[i - j] Math.abs(stones[i - j] - stones[i]) Math.abs Math stones[i - j] - stones[i] stones[i - j] stones i - j i j stones[i] stones i cost[i - j] cost i - j i j cost[i] cost i return cost[stones.length - 1]; cost[stones.length - 1] cost stones.length - 1 stones.length stones stones.length 1 int stones[] stones int k k public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n n k; k String array[] = br.readLine().split(" "); array br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " n = Integer.valueOf(array[0]) n Integer.valueOf(array[0]) Integer.valueOf Integer array[0] array 0 k = Integer.valueOf(array[1]) k Integer.valueOf(array[1]) Integer.valueOf Integer array[1] array 1 int stones[] = new int[n]; stones new int[n] n array = br.readLine().split(" ") array br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			stones[i] = Integer.valueOf(array[i]);
		} {
			stones[i] = Integer.valueOf(array[i]);
		} stones[i] = Integer.valueOf(array[i]) stones[i] stones i Integer.valueOf(array[i]) Integer.valueOf Integer array[i] array i System.out.println("" + minCost(stones, k)) System.out.println System.out System System.out "" + minCost(stones, k) "" minCost(stones, k) minCost stones k String[] args args public class Main {
	public static int minCost(int stones[], int k) {
		int cost[] = new int[stones.length];
		cost[0] = 0;
		cost[1] = Math.abs(stones[1] - stones[0]);
		for (int i = 2; i < stones.length; i++) {
			cost[i] = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				if (i - j >= 0) {
					cost[i] = Math.min(Math.abs(stones[i - j] - stones[i]) + cost[i - j], cost[i]);
				}
			}
		}
		return cost[stones.length - 1];
	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	}

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

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	}

} Main

import java.io.BufferedReader;
import java.io.InputStreamReader;

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

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n, k;
		String array[] = br.readLine().split(" ");
		n = Integer.valueOf(array[0]);
		k = Integer.valueOf(array[1]);
		int stones[] = new int[n];
		array = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			stones[i] = Integer.valueOf(array[i]);
		}
		System.out.println("" + minCost(stones, k));

	}

}