import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
	public static void main (String[] args) {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	}
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {
	public static void main (String[] args) {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	}
} Main public static void main (String[] args) {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	} main {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int K = in.nextInt(); K in.nextInt() in.nextInt in int[] a = new int[N]; a new int[N] N for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
        a[i] = in.nextInt();
      } {
        a[i] = in.nextInt();
      } a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in int[] dp = new int[N]; dp new int[N] N dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(a[0] - a[1]) dp[1] dp 1 Math.abs(a[0] - a[1]) Math.abs Math a[0] - a[1] a[0] a 0 a[1] a 1 for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      } int i = 2; int i = 2; i 2 i< N i N i++ i++ i {
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      } {
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      } int k = 1; k 1 int temp = Integer.MAX_VALUE; temp Integer.MAX_VALUE Integer Integer.MAX_VALUE while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        } k <= K && i-k >=0 k <= K k K i-k >=0 i-k i k 0 {
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        } temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k])) temp Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k])) Math.min Math temp dp[i-k] + Math.abs(a[i] - a[i-k]) dp[i-k] dp i-k i k Math.abs(a[i] - a[i-k]) Math.abs Math a[i] - a[i-k] a[i] a i a[i-k] a i-k i k k++ k dp[i] = temp dp[i] dp i temp 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 in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	}
} public class Main {
	public static void main (String[] args) {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	}
} Main
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
	public static void main (String[] args) {
      Scanner in = new Scanner(System.in);
      int N = in.nextInt();
      int K = in.nextInt();
      int[] a = new int[N];
      for (int i = 0; i < N; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[N];
      dp[0] = 0;
      dp[1] = Math.abs(a[0] - a[1]);
      for(int i = 2; i< N ;i++){
        int k = 1;
        int temp = Integer.MAX_VALUE;
        while(k <= K && i-k >=0){
          temp = Math.min(temp, dp[i-k] + Math.abs(a[i] - a[i-k]));
          k++;
        }
        dp[i] = temp;
      }
      System.out.println(dp[N-1]);
	}
}
