import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	}

	private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	}


} import java.util.*; util.* java import java.io.*; io.* java public class Main {

	public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	}

	private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	}


} Main public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	} main {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	} Main ans = new Main(); ans new Main() System.out.println(ans.solve()) System.out.println System.out System System.out ans.solve() ans.solve ans String[] args args private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	} solve {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	} 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 []array = new int[N+1]; array new int[N+1] N+1 N 1 for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		} int i=1; int i=1; i 1 i<=N i N i++ i++ i {
			array[i] = sc.nextInt();
		} {
			array[i] = sc.nextInt();
		} array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc int [] dp = new int[N+1]; dp new int[N+1] N+1 N 1 dp[1] =0 dp[1] dp 1 0 for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} int i=2; int i=2; i 2 i<=N i N i++ i++ i {
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} {
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			} int j=i-1; int j=i-1; j i-1 i 1 j>=1 && i-j<=K j>=1 j 1 i-j<=K i-j i j K j-- j-- j {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			} {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			} dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]) dp[i] dp i Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]) Math.min Math dp[i] dp i Math.abs(array[i]-array[j])+dp[j] Math.abs(array[i]-array[j]) Math.abs Math array[i]-array[j] array[i] array i array[j] array j dp[j] dp j return dp[N]; dp[N] dp N public class Main {

	public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	}

	private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	}


} public class Main {

	public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	}

	private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	}


} Main
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) {

		Main ans = new Main();
		System.out.println(ans.solve());
		
	}

	private int solve() {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();

		int []array = new int[N+1];
		for(int i=1;i<=N;i++){
			array[i] = sc.nextInt();
		}

		int [] dp = new int[N+1];

		dp[1] =0;

		for(int i=2;i<=N;i++){
			dp[i] = Integer.MAX_VALUE;
			for(int j=i-1;j>=1 && i-j<=K;j--) {
				dp[i] = Math.min(dp[i], Math.abs(array[i]-array[j])+dp[j]);
			}
		} 

		return dp[N];
		 
	}


}