import java.io.*;
 
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	}
	
	public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	}
} import java.io.*; io.* java public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	}
	
	public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	}
} Main public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] line1 = br.readLine().split(" "); line1 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int N = Integer.parseInt(line1[0]); N Integer.parseInt(line1[0]) Integer.parseInt Integer line1[0] line1 0 int K = Integer.parseInt(line1[1]); K Integer.parseInt(line1[1]) Integer.parseInt Integer line1[1] line1 1 int[] array = new int[N+1]; array new int[N+1] N+1 N 1 line1 = br.readLine().split(" ") line1 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			array[i+1] = Integer.parseInt(line1[i]);
		} {
			array[i+1] = Integer.parseInt(line1[i]);
		} array[i+1] = Integer.parseInt(line1[i]) array[i+1] array i+1 i 1 Integer.parseInt(line1[i]) Integer.parseInt Integer line1[i] line1 i System.out.println(metodo(N, K, array)) System.out.println System.out System System.out metodo(N, K, array) metodo N K array String[] args args public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	} metodo {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	} int[] array1 = new int[N+1]; array1 new int[N+1] N+1 N 1 for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		} int i = 2; int i = 2; i 2 i <= N i N i++ i++ i {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		} {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		} array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]) array1[i] array1 i array1[i-1] + Math.abs(array[i-1] - array[i]) array1[i-1] array1 i-1 i 1 Math.abs(array[i-1] - array[i]) Math.abs Math array[i-1] - array[i] array[i-1] array i-1 i 1 array[i] array i for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[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 {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			} {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			} array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i])) array1[i] array1 i Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i])) Math.min Math array1[i] array1 i array1[j] + Math.abs(array[j]-array[i]) array1[j] array1 j Math.abs(array[j]-array[i]) Math.abs Math array[j]-array[i] array[j] array j array[i] array i return array1[N]; array1[N] array1 N int N N int K K int[] array array public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	}
	
	public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	}
} public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	}
	
	public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	}
} Main
import java.io.*;
 
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] line1 = br.readLine().split(" ");
		int N = Integer.parseInt(line1[0]);
		int K = Integer.parseInt(line1[1]);
		int[] array = new int[N+1];
		
		line1 = br.readLine().split(" ");
		for(int i = 0; i < N ; i++) {
			array[i+1] = Integer.parseInt(line1[i]);
		}
		System.out.println(metodo(N, K, array));
	}
	
	public static int metodo(int N, int K, int[] array) {
		int[] array1 = new int[N+1];
		for(int i = 2; i <= N; i++) {
			array1[i] = array1[i-1] + Math.abs(array[i-1] - array[i]);
			for(int j = i-2; j >= i-K && j > 0; j--) {
				array1[i] = Math.min(array1[i], array1[j] + Math.abs(array[j]-array[i]));
			}
		}
		
		return array1[N];
	}
}