import java.io.*;
import java.util.*;

class Main {
  static final Scanner in = new Scanner(System.in);
  static int N, K;

  static int[] h;
  // static int dp[][];
  static int dp[];
  static final int MOD = 1000000007;
  static int[] pre;

  public static void main(String[] args) throws Exception {
    N = in.nextInt();
    K = in.nextInt();
    h = new int[N];
    dp = new int[N];

    // dp = new int[N + 1][K + 1];
    // pre = new int[N];

    int old_sum = 0;
    for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    }
    Arrays.fill(dp, -1);

    System.out.println(F(N - 1));
  }

  // static int F(int i, int k) {
  //   // System.out.println("\tat i == " + i + " k = " + k);
  //   if (i < N && k > pre[i]) return 0;
  //   if (i < N && k == pre[i]) return 1;

  //   if (i == 1) {
  //     // System.out.println("\tat i == 0 k = "+k);
  //     if (k > a[0]) {
  //       // System.out.println("\treturning 0");5
  //       return 0;
  //     }
  //     // System.out.println("\treturning 1");
  //     return 1;
  //   }

  //   if (dp[i][k] != -1) return dp[i][k];

  //   int val = 0;
  //   for (int j = 0; j <= a[i - 1]; j++) {
  //     // System.out.println("\t\tat i == " + i + " k = " + k + " j = " + j);
  //     if (k < j) break;
  //     val += F(i - 1, k - j) % MOD;
  //     val %= MOD;
  //   }
  //   // System.out.println("\t\tat i == " + i + " k = " + k + " val = " + val);
  //   dp[i][k] = val % MOD;
  //   return dp[i][k];
  // }

  static int F(int i) {
    if (i == 0) return 0;

    if (i == 1) return Math.abs(h[0] - h[1]);
    if(dp[i] != -1)
      return dp[i];
    int min_cost = Integer.MAX_VALUE;
    for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    }
    
    dp[i] = min_cost;
    return dp[i];  

  }
  // static int F_topDown(int i) {
  //   for (int i = 0; i < N; i++) {
  //     //..if we have only 1 stone
  //     if (i == 0) {
  //       dp[i] = 0;
  //       continue;
  //     }

  //     //..if we have 2 stones
  //     if (i == 1) {
  //       dp[i] = Math.abs(h[0] - h[1]);
  //       continue;
  //     }

  //     //..if frog jumps from previous stone
  //     int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //     //..if frog jumps from two stones back
  //     int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //     //..store it before returning
  //     dp[i] = Math.min(cost1, cost2);
  //   }
  //   //..if value is already been calculated return directly
  //   if (dp[i] != -1) return dp[i];

  //   //..if frog jumps from previous stone
  //   int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //   //..if frog jumps from two stones back
  //   int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //   //..store it before returning
  //   dp[i] = Math.min(cost1, cost2);
  //   return dp[i];
  // }
} import java.io.*; io.* java import java.util.*; util.* java class Main {
  static final Scanner in = new Scanner(System.in);
  static int N, K;

  static int[] h;
  // static int dp[][];
  static int dp[];
  static final int MOD = 1000000007;
  static int[] pre;

  public static void main(String[] args) throws Exception {
    N = in.nextInt();
    K = in.nextInt();
    h = new int[N];
    dp = new int[N];

    // dp = new int[N + 1][K + 1];
    // pre = new int[N];

    int old_sum = 0;
    for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    }
    Arrays.fill(dp, -1);

    System.out.println(F(N - 1));
  }

  // static int F(int i, int k) {
  //   // System.out.println("\tat i == " + i + " k = " + k);
  //   if (i < N && k > pre[i]) return 0;
  //   if (i < N && k == pre[i]) return 1;

  //   if (i == 1) {
  //     // System.out.println("\tat i == 0 k = "+k);
  //     if (k > a[0]) {
  //       // System.out.println("\treturning 0");5
  //       return 0;
  //     }
  //     // System.out.println("\treturning 1");
  //     return 1;
  //   }

  //   if (dp[i][k] != -1) return dp[i][k];

  //   int val = 0;
  //   for (int j = 0; j <= a[i - 1]; j++) {
  //     // System.out.println("\t\tat i == " + i + " k = " + k + " j = " + j);
  //     if (k < j) break;
  //     val += F(i - 1, k - j) % MOD;
  //     val %= MOD;
  //   }
  //   // System.out.println("\t\tat i == " + i + " k = " + k + " val = " + val);
  //   dp[i][k] = val % MOD;
  //   return dp[i][k];
  // }

  static int F(int i) {
    if (i == 0) return 0;

    if (i == 1) return Math.abs(h[0] - h[1]);
    if(dp[i] != -1)
      return dp[i];
    int min_cost = Integer.MAX_VALUE;
    for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    }
    
    dp[i] = min_cost;
    return dp[i];  

  }
  // static int F_topDown(int i) {
  //   for (int i = 0; i < N; i++) {
  //     //..if we have only 1 stone
  //     if (i == 0) {
  //       dp[i] = 0;
  //       continue;
  //     }

  //     //..if we have 2 stones
  //     if (i == 1) {
  //       dp[i] = Math.abs(h[0] - h[1]);
  //       continue;
  //     }

  //     //..if frog jumps from previous stone
  //     int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //     //..if frog jumps from two stones back
  //     int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //     //..store it before returning
  //     dp[i] = Math.min(cost1, cost2);
  //   }
  //   //..if value is already been calculated return directly
  //   if (dp[i] != -1) return dp[i];

  //   //..if frog jumps from previous stone
  //   int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //   //..if frog jumps from two stones back
  //   int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //   //..store it before returning
  //   dp[i] = Math.min(cost1, cost2);
  //   return dp[i];
  // }
} Main static final Scanner in = new Scanner(System.in); in new Scanner(System.in) static int N N K; K static int[] h; h // static int dp[][];
  static int dp[]; dp static final int MOD = 1000000007; MOD 1000000007 static int[] pre; pre public static void main(String[] args) throws Exception {
    N = in.nextInt();
    K = in.nextInt();
    h = new int[N];
    dp = new int[N];

    // dp = new int[N + 1][K + 1];
    // pre = new int[N];

    int old_sum = 0;
    for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    }
    Arrays.fill(dp, -1);

    System.out.println(F(N - 1));
  } main {
    N = in.nextInt();
    K = in.nextInt();
    h = new int[N];
    dp = new int[N];

    // dp = new int[N + 1][K + 1];
    // pre = new int[N];

    int old_sum = 0;
    for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    }
    Arrays.fill(dp, -1);

    System.out.println(F(N - 1));
  } N = in.nextInt() N in.nextInt() in.nextInt in K = in.nextInt() K in.nextInt() in.nextInt in h = new int[N] h new int[N] N dp = new int[N] dp new int[N] N int old_sum = 0; old_sum 0 for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    } {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    } h[i] = in.nextInt() h[i] h i in.nextInt() in.nextInt in Arrays.fill(dp, -1) Arrays.fill Arrays dp -1 1 System.out.println(F(N - 1)) System.out.println System.out System System.out F(N - 1) F N - 1 N 1 String[] args args static int F(int i) {
    if (i == 0) return 0;

    if (i == 1) return Math.abs(h[0] - h[1]);
    if(dp[i] != -1)
      return dp[i];
    int min_cost = Integer.MAX_VALUE;
    for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    }
    
    dp[i] = min_cost;
    return dp[i];  

  } F {
    if (i == 0) return 0;

    if (i == 1) return Math.abs(h[0] - h[1]);
    if(dp[i] != -1)
      return dp[i];
    int min_cost = Integer.MAX_VALUE;
    for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    }
    
    dp[i] = min_cost;
    return dp[i];  

  } if (i == 0) return 0; i == 0 i 0 return 0; 0 if (i == 1) return Math.abs(h[0] - h[1]); i == 1 i 1 return Math.abs(h[0] - h[1]); Math.abs(h[0] - h[1]) Math.abs Math h[0] - h[1] h[0] h 0 h[1] h 1 if(dp[i] != -1)
      return dp[i]; dp[i] != -1 dp[i] dp i -1 1 return dp[i]; dp[i] dp i int min_cost = Integer.MAX_VALUE; min_cost Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    } int k = 1; int k = 1; k 1 k <= K k K k++ k++ k {
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    } {
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    } if(k > i)
        break; k > i k i break; min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i])) min_cost Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i])) Math.min Math min_cost F(i-k) + Math.abs(h[i-k] - h[i]) F(i-k) F i-k i k Math.abs(h[i-k] - h[i]) Math.abs Math h[i-k] - h[i] h[i-k] h i-k i k h[i] h i dp[i] = min_cost dp[i] dp i min_cost return dp[i]; dp[i] dp i int i i
import java.io.*;
import java.util.*;

class Main {
  static final Scanner in = new Scanner(System.in);
  static int N, K;

  static int[] h;
  // static int dp[][];
  static int dp[];
  static final int MOD = 1000000007;
  static int[] pre;

  public static void main(String[] args) throws Exception {
    N = in.nextInt();
    K = in.nextInt();
    h = new int[N];
    dp = new int[N];

    // dp = new int[N + 1][K + 1];
    // pre = new int[N];

    int old_sum = 0;
    for (int i = 0; i < N; i++) {
      h[i] = in.nextInt();
      //   pre[i] = old_sum + a[i];
      //   old_sum = pre[i];
      // Arrays.fill(dp[i],-1);
    }
    Arrays.fill(dp, -1);

    System.out.println(F(N - 1));
  }

  // static int F(int i, int k) {
  //   // System.out.println("\tat i == " + i + " k = " + k);
  //   if (i < N && k > pre[i]) return 0;
  //   if (i < N && k == pre[i]) return 1;

  //   if (i == 1) {
  //     // System.out.println("\tat i == 0 k = "+k);
  //     if (k > a[0]) {
  //       // System.out.println("\treturning 0");5
  //       return 0;
  //     }
  //     // System.out.println("\treturning 1");
  //     return 1;
  //   }

  //   if (dp[i][k] != -1) return dp[i][k];

  //   int val = 0;
  //   for (int j = 0; j <= a[i - 1]; j++) {
  //     // System.out.println("\t\tat i == " + i + " k = " + k + " j = " + j);
  //     if (k < j) break;
  //     val += F(i - 1, k - j) % MOD;
  //     val %= MOD;
  //   }
  //   // System.out.println("\t\tat i == " + i + " k = " + k + " val = " + val);
  //   dp[i][k] = val % MOD;
  //   return dp[i][k];
  // }

  static int F(int i) {
    if (i == 0) return 0;

    if (i == 1) return Math.abs(h[0] - h[1]);
    if(dp[i] != -1)
      return dp[i];
    int min_cost = Integer.MAX_VALUE;
    for(int k = 1;k <= K;k++){
      if(k > i)
        break;
      min_cost = Math.min(min_cost,F(i-k) + Math.abs(h[i-k] - h[i]));
    }
    
    dp[i] = min_cost;
    return dp[i];  

  }
  // static int F_topDown(int i) {
  //   for (int i = 0; i < N; i++) {
  //     //..if we have only 1 stone
  //     if (i == 0) {
  //       dp[i] = 0;
  //       continue;
  //     }

  //     //..if we have 2 stones
  //     if (i == 1) {
  //       dp[i] = Math.abs(h[0] - h[1]);
  //       continue;
  //     }

  //     //..if frog jumps from previous stone
  //     int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //     //..if frog jumps from two stones back
  //     int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //     //..store it before returning
  //     dp[i] = Math.min(cost1, cost2);
  //   }
  //   //..if value is already been calculated return directly
  //   if (dp[i] != -1) return dp[i];

  //   //..if frog jumps from previous stone
  //   int cost1 = F(i - 1) + Math.abs(h[i - 1] - h[i]);

  //   //..if frog jumps from two stones back
  //   int cost2 = F(i - 2) + Math.abs(h[i - 2] - h[i]);

  //   //..store it before returning
  //   dp[i] = Math.min(cost1, cost2);
  //   return dp[i];
  // }
}
