
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), k = sc.nextInt(), h[] = new int[n];
		long dp[] = new long[n];
		for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		}
		System.out.println(dp[n - 1]);
		sc.close();
	}
} 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(), k = sc.nextInt(), h[] = new int[n];
		long dp[] = new long[n];
		for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		}
		System.out.println(dp[n - 1]);
		sc.close();
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), k = sc.nextInt(), h[] = new int[n];
		long dp[] = new long[n];
		for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		}
		System.out.println(dp[n - 1]);
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), k = sc.nextInt(), h[] = new int[n];
		long dp[] = new long[n];
		for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		}
		System.out.println(dp[n - 1]);
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt() n sc.nextInt() sc.nextInt sc k = sc.nextInt() k sc.nextInt() sc.nextInt sc h[] = new int[n]; h new int[n] n long dp[] = new long[n]; dp new long[n] n for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		} int i = 0; int i = 0; i 0 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[0] = 0 dp[0] dp 0 0 for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		} {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		} for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			} int j = i - 1; int j = i - 1; j i - 1 i 1 j >= 0 && j >= i - k j >= 0 j 0 j >= i - k j i - k i k j-- j-- j {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			} {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			} long a = Integer.MAX_VALUE; a Integer.MAX_VALUE Integer Integer.MAX_VALUE if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]); i - 1 >= 0 i - 1 i 1 0 a = dp[j] + Math.abs(h[j] - h[i]) a dp[j] + Math.abs(h[j] - h[i]) dp[j] dp j Math.abs(h[j] - h[i]) Math.abs Math h[j] - h[i] h[j] h j h[i] h i dp[i] = Long.min(dp[i], a) dp[i] dp i Long.min(dp[i], a) Long.min Long dp[i] dp i a System.out.println(dp[n - 1]) System.out.println System.out System System.out dp[n - 1] dp n - 1 n 1 sc.close() sc.close sc String[] args args public class Main {

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

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

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), k = sc.nextInt(), h[] = new int[n];
		long dp[] = new long[n];
		for (int i = 0; i < n; i++) {
			h[i] = sc.nextInt();
			dp[i] = Integer.MAX_VALUE;
		}
		dp[0] = 0;
		for (int i = 1; i < n; i++) {
			for (int j = i - 1; j >= 0 && j >= i - k; j--) {
				long a = Integer.MAX_VALUE;
				if (i - 1 >= 0)
					a = dp[j] + Math.abs(h[j] - h[i]);
				dp[i] = Long.min(dp[i], a);
			}
		}
		System.out.println(dp[n - 1]);
		sc.close();
	}
}
