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[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	}
} 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[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	} 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[] height = new int[N+1]; height new int[N+1] N+1 N 1 int[] dp = new int[N+1]; dp new int[N+1] N+1 N 1 for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		} int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
			height[i] = sc.nextInt();
		} {
			height[i] = sc.nextInt();
		} height[i] = sc.nextInt() height[i] height i sc.nextInt() sc.nextInt sc dp[1] = 0 dp[1] dp 1 0 dp[2] = Math.abs(height[2]-height[1]) dp[2] dp 2 Math.abs(height[2]-height[1]) Math.abs Math height[2]-height[1] height[2] height 2 height[1] height 1 for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		} int i = 3; int i = 3; i 3 i <= N i N i++ i++ i {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		} {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			} int j = 1; int j = 1; j 1 j <= K && i - j >= 1 j <= K j K i - j >= 1 i - j i j 1 j++ j++ j {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			} {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			} int cost = Math.abs(height[i]-height[i-j]); cost Math.abs(height[i]-height[i-j]) Math.abs Math height[i]-height[i-j] height[i] height i height[i-j] height i-j i j min = Math.min(min,cost+dp[i-j]) min Math.min(min,cost+dp[i-j]) Math.min Math min cost+dp[i-j] cost dp[i-j] dp i-j i j dp[i] = min dp[i] dp i min System.out.println(dp[N]) System.out.println System.out System System.out dp[N] dp N 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[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	}
} public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	}
} 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[] height = new int[N+1];
		int[] dp = new int[N+1];
		for(int i = 1; i <= N; i++) {
			height[i] = sc.nextInt();
		}
		dp[1] = 0;
		dp[2] = Math.abs(height[2]-height[1]);
		for(int i = 3; i <= N; i++) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= K && i - j >= 1; j++) {
				int cost = Math.abs(height[i]-height[i-j]);
				min = Math.min(min,cost+dp[i-j]);

			}
			dp[i] = min;
		}
		System.out.println(dp[N]);
	}
}