import java.util.*;
import java.util.stream.IntStream;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();
    int K = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = sc.nextInt();
    int[] dp = new int[N];
    int b = K;
    for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    }
    System.out.println(dp[N-1]);
  }
} import java.util.*; util.* java import java.util.stream.IntStream; IntStream java.util.stream class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();
    int K = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = sc.nextInt();
    int[] dp = new int[N];
    int b = K;
    for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    }
    System.out.println(dp[N-1]);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();
    int K = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = sc.nextInt();
    int[] dp = new int[N];
    int b = K;
    for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    }
    System.out.println(dp[N-1]);
  } main {
    Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();
    int K = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = sc.nextInt();
    int[] dp = new int[N];
    int b = K;
    for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    }
    System.out.println(dp[N-1]);
  } 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[] a = new int[N]; a new int[N] N for (int i = 0; i < N; i++) a[i] = sc.nextInt(); int i = 0; int i = 0; i 0 i < N i N i++ i++ i a[i] = sc.nextInt(); a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int[] dp = new int[N]; dp new int[N] N int b = K; b K for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    } {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    } if (i <= K) b--; i <= K i K b-- b for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      } int j = 1; int j = 1; j 1 j <= K-b j K-b K b j++ j++ j {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      } {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      } if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        } j == 1 j 1 {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]) dp[i] dp i dp[i-j] + Math.abs(a[i] - a[i-j]) dp[i-j] dp i-j i j Math.abs(a[i] - a[i-j]) Math.abs Math a[i] - a[i-j] a[i] a i a[i-j] a i-j i j {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        } dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j])) dp[i] dp i Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j])) Math.min Math dp[i] dp i dp[i-j] + Math.abs(a[i] - a[i-j]) dp[i-j] dp i-j i j Math.abs(a[i] - a[i-j]) Math.abs Math a[i] - a[i-j] a[i] a i a[i-j] a i-j i j 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
import java.util.*;
import java.util.stream.IntStream;
class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	int N = sc.nextInt();
    int K = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = sc.nextInt();
    int[] dp = new int[N];
    int b = K;
    for (int i = 1; i < N; i++) {
      if (i <= K) b--;
      for (int j = 1; j <= K-b; j++) {
        if (j == 1) {
          dp[i] = dp[i-j] + Math.abs(a[i] - a[i-j]);
        } else {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(a[i] - a[i-j]));
        }
      }
    }
    System.out.println(dp[N-1]);
  }
}