import java.util.*;

public class Main{
	void solve(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	}

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

	public static void main(String[] args){
		new Main().solve();
	}
} Main void solve(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	} solve {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int[] h = new int[n]; h new int[n] n for(int i = 0; i < n; i++) h[i] = scan.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i h[i] = scan.nextInt(); h[i] = scan.nextInt() h[i] h i scan.nextInt() scan.nextInt scan int[] dp = new int[n]; dp new int[n] n for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE; int i = 0; int i = 0; i 0 i < n i n i++ i++ i dp[i] = Integer.MAX_VALUE; dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(h[1] - h[0]) dp[1] dp 1 Math.abs(h[1] - h[0]) Math.abs Math h[1] - h[0] h[1] h 1 h[0] h 0 for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		} {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		} for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			} {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			} if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i])); i - j >= 0 i - j i j 0 dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i])) dp[i] dp i Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i])) Math.min Math dp[i] dp i dp[i - j] + Math.abs(h[i - j] - h[i]) dp[i - j] dp i - j i j Math.abs(h[i - j] - h[i]) Math.abs Math h[i - j] - h[i] h[i - j] h i - j i j h[i] h i System.out.println(dp[n - 1]) System.out.println System.out System System.out dp[n - 1] dp n - 1 n 1 scan.close() scan.close scan public static void main(String[] args){
		new Main().solve();
	} main {
		new Main().solve();
	} new Main().solve() new Main().solve new Main() String[] args args public class Main{
	void solve(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	}

	public static void main(String[] args){
		new Main().solve();
	}
} public class Main{
	void solve(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	}

	public static void main(String[] args){
		new Main().solve();
	}
} Main
import java.util.*;

public class Main{
	void solve(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int k = scan.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++) h[i] = scan.nextInt();
		int[] dp = new int[n];
		for(int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
		dp[0] = 0;
		dp[1] = Math.abs(h[1] - h[0]);
		for(int i = 0; i < n; i++) {
			for(int j = 1; j <= k; j++) {
				if(i - j >= 0) dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(h[i - j] - h[i]));
			}
		}
		System.out.println(dp[n - 1]);
		scan.close();
	}

	public static void main(String[] args){
		new Main().solve();
	}
}
 