import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

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

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	} main {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc sc.nextLine() sc.nextLine sc int[] h = new int[n + 1]; h new int[n + 1] n + 1 n 1 int[] dp = new int[n + 1]; dp new int[n + 1] n + 1 n 1 h[0] = Integer.MAX_VALUE h[0] h 0 Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		} {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		} h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[1] = 0 dp[1] dp 1 0 for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		} int j = 1; int j = 1; j 1 j < n j n j++ j++ j {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		} {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		} for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			} int a = 1; int a = 1; a 1 a <= k a k a++ a++ a {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			} {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			} if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				} j + a <= n j + a j a n {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				} dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a])) dp[j + a] dp j + a j a Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a])) Math.min Math dp[j + a] dp j + a j a dp[j] + Math.abs(h[j] - h[j + a]) dp[j] dp j Math.abs(h[j] - h[j + a]) Math.abs Math h[j] - h[j + a] h[j] h j h[j + a] h j + a j a System.out.println(dp[n]) System.out.println System.out System System.out dp[n] dp n String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	}
} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	}
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();

		sc.nextLine();

		int[] h = new int[n + 1];
		int[] dp = new int[n + 1];

		h[0] = Integer.MAX_VALUE;
		for (int i = 1; i <= n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}

		dp[1] = 0;
		for (int j = 1; j < n; j++) {
			for (int a = 1; a <= k; a++) {
				if (j + a <= n) {
					dp[j + a] = Math.min(dp[j + a], dp[j] + Math.abs(h[j] - h[j + a]));
				}
			}
		}

		System.out.println(dp[n]);

	}
}
