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

public class Main {
  
   public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   }
  
   private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
  
   public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   }
  
   private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   }
} Main public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   } main {
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   } Main ans = new Main(); ans new Main() int sol = ans.solve(); sol ans.solve() ans.solve ans System.out.println(sol) System.out.println System.out System System.out sol String[]abc abc private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   } solve {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   } 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 [] array = new int[N+1]; array new int[N+1] N+1 N 1 for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      } int i=1; int i=1; i 1 i<=N i N i++ i++ i {
         array[i] = sc.nextInt();        
      } {
         array[i] = sc.nextInt();        
      } array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc int [] dp = new int[N+1]; dp new int[N+1] N+1 N 1 Arrays.fill(dp, Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[1]=0 dp[1] dp 1 0 dp[0]=0 dp[0] dp 0 0 for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      } int i=2; int i=2; i 2 i<=N i N i++ i++ i {
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      } {
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      } for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         } int j=i-1; int j=i-1; j i-1 i 1 j>=1 && i-j<=K j>=1 j 1 i-j<=K i-j i j K j-- j-- j {
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         } {
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         } dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]) dp[i] dp i Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]) Math.min Math dp[j]+ Math.abs(array[j]-array[i]) dp[j] dp j Math.abs(array[j]-array[i]) Math.abs Math array[j]-array[i] array[j] array j array[i] array i dp[i] dp i return dp[N]; dp[N] dp N public class Main {
  
   public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   }
  
   private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   }
} public class Main {
  
   public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   }
  
   private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   }
} Main
import java.util.*;
import java.io.*;

public class Main {
  
   public static void main(String[]abc){
      Main ans = new Main();
      int sol = ans.solve();
      System.out.println(sol);
   }
  
   private int solve() {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      int K = sc.nextInt();
      int [] array = new int[N+1];
      for(int i=1;i<=N;i++) {
         array[i] = sc.nextInt();        
      }
      int [] dp = new int[N+1];
      Arrays.fill(dp, Integer.MAX_VALUE);
      dp[1]=0;
      dp[0]=0;
      for(int i=2;i<=N;i++){
         for(int j=i-1;j>=1 && i-j<=K;j--){
            dp[i] = Math.min(dp[j]+ Math.abs(array[j]-array[i]), dp[i]);
         }
      }
	//System.out.println(Arrays.toString(dp));
     return dp[N];
      
      
   }
}