import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} import java.util.*; util.* java public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} Main public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	} 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[] h = new int[N]; h new int[N] N int[] dp = new int[N]; dp new int[N] N for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			h[i] = sc.nextInt();
		} {
			h[i] = sc.nextInt();
		} h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc dp[0] = 0 dp[0] dp 0 0 dp[1] = (int)Math.abs(h[1] - h[0]) dp[1] dp 1 (int)Math.abs(h[1] - h[0]) Math.abs Math h[1] - h[0] h[1] h 1 h[0] h 0 for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		} int i = 2; int i = 2; i 2 i < N i N i++ i++ i {
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		} {
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		} dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]) dp[i] dp i dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]) dp[i - 1] dp i - 1 i 1 (int)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 = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			} int j = 1; int j = 1; j 1 j < K + 1 j K + 1 K 1 j++ j++ j {
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			} {
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			} if(i - j < 0){
					break;
				} i - j < 0 i - j i j 0 {
					break;
				} break; dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]) dp[i] dp i Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]) Math.min Math dp[i - j] + (int)Math.abs(h[i - j] - h[i]) dp[i - j] dp i - j i j (int)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 dp[i] dp i 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 sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
} Main
import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] h = new int[N];
		int[] dp = new int[N];
		for(int i = 0;i < N;i++){
			h[i] = sc.nextInt();
		}
		dp[0] = 0;
		dp[1] = (int)Math.abs(h[1] - h[0]);
		for(int i = 2;i < N;i++){
			dp[i] = dp[i - 1] + (int)Math.abs(h[i - 1] - h[i]);
			for(int j = 1;j < K + 1;j++){
				if(i - j < 0){
					break;
				}
				dp[i] = Math.min(dp[i - j] + (int)Math.abs(h[i - j] - h[i]),dp[i]);
			}
		}
		System.out.println(dp[N-1]);
	}
}