

import java.util.Scanner;

public class Main {

	static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	}

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	}

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

	static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	}

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	}

} Main static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	} mincost {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	} if (i == ar.length - 1)
			return 0; i == ar.length - 1 i ar.length - 1 ar.length ar ar.length 1 return 0; 0 if (dp[i] != -1)
			return dp[i]; dp[i] != -1 dp[i] dp i -1 1 return dp[i]; dp[i] dp i int mincost = Integer.MAX_VALUE; mincost Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp))); int index = i + 1; int index = i + 1; index i + 1 i 1 index <= Math.min(ar.length - 1, i + k) index Math.min(ar.length - 1, i + k) Math.min Math ar.length - 1 ar.length ar ar.length 1 i + k i k index++ index++ index mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp))); mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp))) mincost Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp))) Math.min Math mincost (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)) Math.abs(ar[i] - ar[index]) Math.abs Math ar[i] - ar[index] ar[i] ar i ar[index] ar index mincost(index, k, ar, dp) mincost index k ar dp return dp[i] = mincost; dp[i] = mincost dp[i] dp i mincost int i i int k k int[] ar ar int[] dp dp public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	} 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 int ar[] = new int[n]; ar new int[n] n int dp[] = new int[n]; dp new int[n] n for (int i = 0; i < n; i++) {
			ar[i] = sc.nextInt();
			dp[i] = -1;
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			ar[i] = sc.nextInt();
			dp[i] = -1;
		} {
			ar[i] = sc.nextInt();
			dp[i] = -1;
		} ar[i] = sc.nextInt() ar[i] ar i sc.nextInt() sc.nextInt sc dp[i] = -1 dp[i] dp i -1 1 System.out.println(mincost(0, k, ar, dp)) System.out.println System.out System System.out mincost(0, k, ar, dp) mincost 0 k ar dp sc.close() sc.close sc String args[] args public class Main {

	static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	}

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	}

} public class Main {

	static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	}

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	}

} Main


import java.util.Scanner;

public class Main {

	static int mincost(int i, int k, int[] ar, int[] dp) {

		if (i == ar.length - 1)
			return 0;

		if (dp[i] != -1)
			return dp[i];

		int mincost = Integer.MAX_VALUE;
		
		for(int index = i + 1; index <= Math.min(ar.length - 1, i + k); index++)
			mincost = Math.min(mincost, (Math.abs(ar[i] - ar[index]) + mincost(index, k, ar, dp)));

		return dp[i] = mincost;
	}

	public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int ar[] = new int[n];
		int dp[] = new int[n];

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

		System.out.println(mincost(0, k, ar, dp));
		sc.close();
	}

}