import java.util.*;
class Main {
    final int INF = 1000000009;
    int[] arr;
    private int cost(int i, int j) {
    	return Math.abs(arr[i] - arr[j]);
	}

	private void run() {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        arr = new int[N];
        for (int i = 0; i < N; i++) arr[i] = sc.nextInt();
        int[] dp = new int[N];
        dp[N-1] = 0;
        for (int i = N - 2; i >= 0; i--) {
            int res = INF;
            for (int j = 1; j <= K; j++) {
                if (i + j >= N) break;
                res = Math.min(res, dp[i + j] + cost(i, i + j));
            }
        	dp[i] = res;
        }
		System.out.println(dp[0]);
    }
  
	public static void main(String[] args) {
    	Main m = new Main();
      	m.run();
	}
} import java.util.*; util.* java class Main {
    final int INF = 1000000009;
    int[] arr;
    private int cost(int i, int j) {
    	return Math.abs(arr[i] - arr[j]);
	}

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

	private void run() {
		Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();
        arr = new int[N];
        for (int i = 0; i < N; i++) arr[i] = sc.nextInt();
        int[] dp = new int[N];
        dp[N-1] = 0;
        for (int i = N - 2; i >= 0; i--) {
            int res = INF;
            for (int j = 1; j <= K; j++) {
                if (i + j >= N) break;
                res = Math.min(res, dp[i + j] + cost(i, i + j));
            }
        	dp[i] = res;
        }
		System.out.println(dp[0]);
    }
  
	public static void main(String[] args) {
    	Main m = new Main();
      	m.run();
	}
}
