import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	}


} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	}


} Main public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	} main {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	} try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		} {


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		} Scanner scan = new Scanner(System.in) Scanner scan = new Scanner(System.in) new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan int K = scan.nextInt(); K scan.nextInt() scan.nextInt scan long[]h = new long[N]; h new long[N] N for(int i = 0;i<N;i++)h[i] =scan.nextLong(); int i = 0; int i = 0; i 0 i<N i N i++ i++ i h[i] =scan.nextLong(); h[i] =scan.nextLong() h[i] h i scan.nextLong() scan.nextLong scan long[]dp = new long[N+1]; dp new long[N+1] N+1 N 1 for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			} int i = 1; int i = 1; i 1 i<N i N i++ i++ i {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			} {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			} if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				} i-K>=0 i-K i K 0 {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				} long dpcost = Long.MAX_VALUE; dpcost Long.MAX_VALUE Long Long.MAX_VALUE int dpnum = 0; dpnum 0 for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					} int j = 1; int j = 1; j 1 j<=K j K j++ j++ j {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					} {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					} if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						} dpcost>dp[i-j]+Math.abs(h[i]-h[i-j]) dpcost dp[i-j]+Math.abs(h[i]-h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						} dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]) dpcost dp[i-j]+Math.abs(h[i]-h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j dpnum = j dpnum j dp[i] =dpcost dp[i] dp i dpcost {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				} long dpcost = Long.MAX_VALUE; dpcost Long.MAX_VALUE Long Long.MAX_VALUE int dpnum = 0; dpnum 0 for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					} int j = 1; int j = 1; j 1 j<=i j i j++ j++ j {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					} {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					} if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						} dpcost>dp[i-j]+Math.abs(h[i]-h[i-j]) dpcost dp[i-j]+Math.abs(h[i]-h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						} dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]) dpcost dp[i-j]+Math.abs(h[i]-h[i-j]) dp[i-j] dp i-j i j Math.abs(h[i]-h[i-j]) Math.abs Math h[i]-h[i-j] h[i] h i h[i-j] h i-j i j dpnum = j dpnum j dp[i] =dpcost dp[i] dp i dpcost 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) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	}


} public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	}


} Main
import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){


			int N = scan.nextInt();
			int K = scan.nextInt();
			long[]h = new long[N];
			for(int i = 0;i<N;i++)h[i] =scan.nextLong();

			long[]dp = new long[N+1];

			for(int i = 1;i<N;i++) {
				if(i-K>=0){
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=K;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}
					}

					dp[i] =dpcost;

				}else {
					long dpcost = Long.MAX_VALUE;
					int dpnum = 0;
					for(int j = 1;j<=i;j++) {
						if(dpcost>dp[i-j]+Math.abs(h[i]-h[i-j])) {
							dpcost = dp[i-j]+Math.abs(h[i]-h[i-j]);
							dpnum = j;
						}

					}


					dp[i] =dpcost;

				}
			}

			System.out.println(dp[N-1]);





		}


	}


}
