import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	}

} import java.util.*; util.* java import java.io.*; io.* java public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	}

} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s = br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n = Integer.parseInt(s[0]); n Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int k = Integer.parseInt(s[1]); k Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 s = br.readLine().split(" ") s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] h = new int[n]; h new int[n] n for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i h[i] = Integer.parseInt(s[i]); h[i] = Integer.parseInt(s[i]) h[i] h i Integer.parseInt(s[i]) Integer.parseInt Integer s[i] s i if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		} n == 2 n 2 {
			System.out.println(h[1] - h[0]);
			return;
		} System.out.println(h[1] - h[0]) System.out.println System.out System System.out h[1] - h[0] h[1] h 1 h[0] h 0 return; int[] min = new int[n]; min new int[n] n for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		} {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		} int m = Integer.MAX_VALUE; m Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			} {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			} int x = Math.abs(h[i] - h[i - j]) + min[i-j]; x Math.abs(h[i] - h[i - j]) + min[i-j] Math.abs(h[i] - h[i - j]) Math.abs Math h[i] - h[i - j] h[i] h i h[i - j] h i - j i j min[i-j] min i-j i j if (m > x) {
					m = x;
				} m > x m x {
					m = x;
				} m = x m x if (i - j == 0)
					break; i - j == 0 i - j i j 0 break; min[i] = m min[i] min i m System.out.println(min[n-1]) System.out.println System.out System System.out min[n-1] min n-1 n 1 String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	}

} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	}

} Main
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);
		s = br.readLine().split(" ");
		int[] h = new int[n];
		for (int i = 0; i < n; i++)
			h[i] = Integer.parseInt(s[i]);

		if (n == 2) {
			System.out.println(h[1] - h[0]);
			return;
		}

		int[] min = new int[n];

		for (int i = 1; i < n; i++) {
			int m = Integer.MAX_VALUE;
			for (int j = 1; j <= k; j++) {
				int x = Math.abs(h[i] - h[i - j]) + min[i-j];
				if (m > x) {
					m = x;
				}
				if (i - j == 0)
					break;
			}
			min[i] = m;
		}

		System.out.println(min[n-1]);
	}

}
