import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	}
	
	public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	}
	
	public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	}
} Main public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] S = br.readLine().split(" "); S br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int N = Integer.parseInt(S[0]); N Integer.parseInt(S[0]) Integer.parseInt Integer S[0] S 0 int K = Integer.parseInt(S[1]); K Integer.parseInt(S[1]) Integer.parseInt Integer S[1] S 1 int[] H = new int[N+1]; H new int[N+1] N+1 N 1 S = br.readLine().split(" ") S br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			H[i+1] = Integer.parseInt(S[i]);
		} {
			H[i+1] = Integer.parseInt(S[i]);
		} H[i+1] = Integer.parseInt(S[i]) H[i+1] H i+1 i 1 Integer.parseInt(S[i]) Integer.parseInt Integer S[i] S i System.out.println(minCostJump(N, K, H)) System.out.println System.out System System.out minCostJump(N, K, H) minCostJump N K H String[] args args public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	} minCostJump {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	} int[] cache = new int[N+1]; cache new int[N+1] N+1 N 1 for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		} int i = 2; int i = 2; i 2 i <= N i N i++ i++ i {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		} {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		} cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]) cache[i] cache i cache[i-1] + Math.abs(H[i-1] - H[i]) cache[i-1] cache i-1 i 1 Math.abs(H[i-1] - H[i]) Math.abs Math H[i-1] - H[i] H[i-1] H i-1 i 1 H[i] H i for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			} int j = i-2; int j = i-2; j i-2 i 2 j >= i-K && j > 0 j >= i-K j i-K i K j > 0 j 0 j-- j-- j {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			} {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			} cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i])) cache[i] cache i Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i])) Math.min Math cache[i] cache i cache[j] + Math.abs(H[j]-H[i]) cache[j] cache j Math.abs(H[j]-H[i]) Math.abs Math H[j]-H[i] H[j] H j H[i] H i return cache[N]; cache[N] cache N int N N int K K int[] H H public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	}
	
	public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	}
} public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	}
	
	public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] S = br.readLine().split(" ");
		int N = Integer.parseInt(S[0]);
		int K = Integer.parseInt(S[1]);
		int[] H = new int[N+1];
		
		S = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			H[i+1] = Integer.parseInt(S[i]);
		}
		System.out.println(minCostJump(N, K, H));
	}
	
	public static int minCostJump(int N, int K, int[] H) {
		int[] cache = new int[N+1];
		
		for(int i = 2; i <= N; i++) {
			cache[i] = cache[i-1] + Math.abs(H[i-1] - H[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				cache[i] = Math.min(cache[i], cache[j] + Math.abs(H[j]-H[i]));
			}
		}
		
		return cache[N];
	}
}
