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();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	}
} 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();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	} 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[] stepInfo = new int[n]; stepInfo new int[n] n for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		} int i =0; int i =0; i 0 i < n i n i++ i++ i {
			stepInfo[i] = sc.nextInt();
		} {
			stepInfo[i] = sc.nextInt();
		} stepInfo[i] = sc.nextInt() stepInfo[i] stepInfo i sc.nextInt() sc.nextInt sc int[] minimumCostInfo = new int[n]; minimumCostInfo new int[n] n minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]) minimumCostInfo[1] minimumCostInfo 1 Math.abs(stepInfo[1]-stepInfo[0]) Math.abs Math stepInfo[1]-stepInfo[0] stepInfo[1] stepInfo 1 stepInfo[0] stepInfo 0 for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		} int i =2; int i =2; i 2 i < n i n i++ i++ i {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		} {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		} int minimumCost = Integer.MAX_VALUE; minimumCost Integer.MAX_VALUE Integer Integer.MAX_VALUE int stepCount = k; stepCount k if(stepCount > i) {
				stepCount = i;
			} stepCount > i stepCount i {
				stepCount = i;
			} stepCount = i stepCount i for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			} int j =1; int j =1; j 1 j <= stepCount j stepCount j++ j++ j {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			} {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			} int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]); cost minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]) minimumCostInfo[i-j] minimumCostInfo i-j i j Math.abs(stepInfo[i]-stepInfo[i-j]) Math.abs Math stepInfo[i]-stepInfo[i-j] stepInfo[i] stepInfo i stepInfo[i-j] stepInfo i-j i j if(cost < minimumCost) {
					minimumCost = cost;
				} cost < minimumCost cost minimumCost {
					minimumCost = cost;
				} minimumCost = cost minimumCost cost minimumCostInfo[i] = minimumCost minimumCostInfo[i] minimumCostInfo i minimumCost System.out.println(minimumCostInfo[n-1]) System.out.println System.out System System.out minimumCostInfo[n-1] minimumCostInfo n-1 n 1 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();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	}
} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	}
} 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();
		int[] stepInfo = new int[n];
		for(int i =0;i < n; i++) {
			stepInfo[i] = sc.nextInt();
		}
		int[] minimumCostInfo = new int[n];
		minimumCostInfo[1] = Math.abs(stepInfo[1]-stepInfo[0]);

		for(int i =2; i < n;i++) {
			int minimumCost = Integer.MAX_VALUE;
			int stepCount = k;
			if(stepCount > i) {
				stepCount = i;
			}
			for(int j =1; j <= stepCount; j++ ) {
				int cost = minimumCostInfo[i-j] + Math.abs(stepInfo[i]-stepInfo[i-j]);
				if(cost < minimumCost) {
					minimumCost = cost;
				}
			}
			minimumCostInfo[i] = minimumCost;
		}

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

	}
}