import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	}
} import java.util.*; util.* java public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	}
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	} main {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	} int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] arr = new int[n]; arr new int[n] n int[] dp = new int[n]; dp new int[n] n for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i arr[i] = sc.nextInt(); arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc Arrays.fill(dp, 0) Arrays.fill Arrays dp 0 if(n == 1) {
			System.out.println(arr[0]);
			return;
		} n == 1 n 1 {
			System.out.println(arr[0]);
			return;
		} System.out.println(arr[0]) System.out.println System.out System System.out arr[0] arr 0 return; for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		} {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			} int j = Math.max(0, i - k); int j = Math.max(0, i - k); j Math.max(0, i - k) Math.max Math 0 i - k i k j < i j i j++ j++ j {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			} {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			} min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]) min Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]) Math.min Math min Math.abs(arr[i] - arr[j]) + dp[j] Math.abs(arr[i] - arr[j]) Math.abs Math arr[i] - arr[j] arr[i] arr i arr[j] arr j dp[j] dp j dp[i] = min dp[i] dp i min System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 return; String[] args args public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	}
} public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	}
} Main
import java.util.*;

public class Main {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] arr = new int[n];
		int[] dp = new int[n];
		
		for(int i = 0;i < n;i++)
			arr[i] = sc.nextInt();
		
		Arrays.fill(dp, 0);
		
		if(n == 1) {
			System.out.println(arr[0]);
			return;
		}
		
		
		for(int i = 1;i < n;i++) {
			int min = Integer.MAX_VALUE;
			for(int j = Math.max(0, i - k);j < i;j++) {
				min = Math.min(min, Math.abs(arr[i] - arr[j]) + dp[j]);
			}
			dp[i] = min;
		}
		
		System.out.println(dp[n-1]);
		return;
		
	}
}
