import java.util.Scanner;

class Main {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int N = sc.nextInt(),i, h[] = new int[N],K=sc.nextInt();
		for(i=0;i<N;i++)h[i]=sc.nextInt();
		int dp[]=new int[N+1];
		dp[0]=0;
		for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} import java.util.Scanner; Scanner java.util class Main {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int N = sc.nextInt(),i, h[] = new int[N],K=sc.nextInt();
		for(i=0;i<N;i++)h[i]=sc.nextInt();
		int dp[]=new int[N+1];
		dp[0]=0;
		for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
		int N = sc.nextInt(),i, h[] = new int[N],K=sc.nextInt();
		for(i=0;i<N;i++)h[i]=sc.nextInt();
		int dp[]=new int[N+1];
		dp[0]=0;
		for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	} main {
		int N = sc.nextInt(),i, h[] = new int[N],K=sc.nextInt();
		for(i=0;i<N;i++)h[i]=sc.nextInt();
		int dp[]=new int[N+1];
		dp[0]=0;
		for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	} int N = sc.nextInt() N sc.nextInt() sc.nextInt sc i i h[] = new int[N] h new int[N] N K=sc.nextInt(); K sc.nextInt() sc.nextInt sc for(i=0;i<N;i++)h[i]=sc.nextInt(); i=0; i=0 i 0 i<N i N i++ i++ i h[i]=sc.nextInt(); h[i]=sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int dp[]=new int[N+1]; dp new int[N+1] N+1 N 1 dp[0]=0 dp[0] dp 0 0 for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		} i=1; i=1 i 1 i<N i N i++ i++ i {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		} {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		} dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]) dp[i] dp i dp[i-1]+Math.abs(h[i]-h[i-1]) dp[i-1] dp i-1 i 1 Math.abs(h[i]-h[i-1]) Math.abs Math h[i]-h[i-1] h[i] h i h[i-1] h i-1 i 1 for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			} int k=1; int k=1; k 1 k<=Math.min(K, i) k Math.min(K, i) Math.min Math K i k++ k++ k {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			} {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			} dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]) dp[i] dp i Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]) Math.min Math dp[i-k]+Math.abs(h[i]-h[i-k]) dp[i-k] dp i-k i k Math.abs(h[i]-h[i-k]) Math.abs Math h[i]-h[i-k] h[i] h i h[i-k] h i-k i k dp[i] dp i System.out.println(dp[N-1]) System.out.println System.out System System.out dp[N-1] dp N-1 N 1 String[] args args
import java.util.Scanner;

class Main {

	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		int N = sc.nextInt(),i, h[] = new int[N],K=sc.nextInt();
		for(i=0;i<N;i++)h[i]=sc.nextInt();
		int dp[]=new int[N+1];
		dp[0]=0;
		for(i=1;i<N;i++) {
			dp[i]=dp[i-1]+Math.abs(h[i]-h[i-1]);
			for(int k=1;k<=Math.min(K, i);k++) {
				dp[i]=Math.min(dp[i-k]+Math.abs(h[i]-h[i-k]), dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
}