import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	}
} Main public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	} main {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	} Scanner kb = new Scanner(System.in); kb new Scanner(System.in) int n = kb.nextInt() n kb.nextInt() kb.nextInt kb k = kb.nextInt(); k kb.nextInt() kb.nextInt kb int[] h = new int[n]; h new int[n] n for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		} int i=0; int i=0; i 0 i < n i n i++ i++ i {
			h[i] = kb.nextInt();
		} {
			h[i] = kb.nextInt();
		} h[i] = kb.nextInt() h[i] h i kb.nextInt() kb.nextInt kb int[] dp = new int[n]; dp new int[n] n boolean[] visited = new boolean[n]; visited new boolean[n] n dp[0] = 0 dp[0] dp 0 0 visited[0] = true visited[0] visited 0 true for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		} {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		} for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			} int j = 1; int j = 1; j 1 j <= k && j <= i j <= k j k j <= i j i j++ j++ j {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			} {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			} int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]); ksteps 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 if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps); !visited[i] visited[i] visited i {
					dp[i] = ksteps;
					visited[i] = true;
				} dp[i] = ksteps dp[i] dp i ksteps visited[i] = true visited[i] visited i true dp[i] = Math.min(dp[i], ksteps) dp[i] dp i Math.min(dp[i], ksteps) Math.min Math dp[i] dp i ksteps 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 public class Main {
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt(), k = kb.nextInt();
		int[] h = new int[n];
		for(int i=0; i < n; i++) {
			h[i] = kb.nextInt();
		}
		int[] dp = new int[n];
	//	Arrays.fill(dp, -1);
		boolean[] visited = new boolean[n];

		dp[0] = 0;
		visited[0] = true;
		for(int i = 1; i < n; i++) {
			for(int j = 1; j <= k && j <= i; j++) {
				int ksteps = dp[i-j] + Math.abs(h[i-j]-h[i]);
				if(!visited[i]) {
					dp[i] = ksteps;
					visited[i] = true;
				}
				else
					dp[i] = Math.min(dp[i], ksteps);
			}
		}
		System.out.println(dp[n-1]);
	}
}
