import java.util.*;
import java.util.function.*;
import java.math.*;
public class Main {
    public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    }
} import java.util.*; util.* java import java.util.function.*; function.* java.util import java.math.*; math.* java public class Main {
    public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    }
} Main public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    } main {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int N = s.nextInt(); N s.nextInt() s.nextInt s int K = s.nextInt(); K s.nextInt() s.nextInt s int[] H = new int[N]; H new int[N] N for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			H[i] = s.nextInt();
		} {
			H[i] = s.nextInt();
		} H[i] = s.nextInt() H[i] H i s.nextInt() s.nextInt s int[] result = new int[N]; result new int[N] N result[0] = 0 result[0] result 0 0 for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		} {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		} result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]) result[i] result i result[i - 1] + Math.abs(H[i] - H[i - 1]) result[i - 1] result i - 1 i 1 Math.abs(H[i] - H[i - 1]) Math.abs Math H[i] - H[i - 1] H[i] H i H[i - 1] H i - 1 i 1 for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		} int n = 0; int n = 0; n 0 n < N n N n++ n++ n {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		} {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		} for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			} int k = 1; int k = 1; k 1 k < K + 1 k K + 1 K 1 k++ k++ k {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			} {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			} if(n + k >= N)
					continue; n + k >= N n + k n k N continue; result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]) result[k + n] result k + n k n Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]) Math.min Math result[n] + Math.abs(H[n] - H[n + k]) result[n] result n Math.abs(H[n] - H[n + k]) Math.abs Math H[n] - H[n + k] H[n] H n H[n + k] H n + k n k result[k + n] result k + n k n System.out.println(result[N-1]) System.out.println System.out System System.out result[N-1] result N-1 N 1 String[] args args public class Main {
    public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    }
} public class Main {
    public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    }
} Main
import java.util.*;
import java.util.function.*;
import java.math.*;
public class Main {
    public static void main(String[] args){
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		int K = s.nextInt();
		int[] H = new int[N];
		for(int i = 0; i < N; i++) {
			H[i] = s.nextInt();
		}
		
		int[] result = new int[N];
		result[0] = 0;
		for(int i = 1; i < N; i++) {
			result[i] = result[i - 1] + Math.abs(H[i] - H[i - 1]);
		}
		for(int n = 0; n < N; n++) {
			for(int k = 1; k < K + 1; k++) {
				if(n + k >= N)
					continue;
				result[k + n] = Math.min(result[n] + Math.abs(H[n] - H[n + k]), result[k + n]);
			}
		}
		System.out.println(result[N-1]);
    }
}