import java.util.Scanner;
import java.lang.Math;

class Main{
  
  	static int solve(int n, int k, int[] arr){
    	int[] dp = new int[n];
      	dp[0] = 0;
        dp[1] = Math.abs(arr[0] - arr[1]);
      
      	for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        }
      
      	return dp[n-1];
    }

	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
      	int n = scanner.nextInt();
      	int k = scanner.nextInt(); scanner.nextLine();
      	int[] arr = new int[n];
      	
      	for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        }
      
      	System.out.println(Main.solve(n,k,arr));
      
      	scanner.close();
    }
} import java.util.Scanner; Scanner java.util import java.lang.Math; Math java.lang class Main{
  
  	static int solve(int n, int k, int[] arr){
    	int[] dp = new int[n];
      	dp[0] = 0;
        dp[1] = Math.abs(arr[0] - arr[1]);
      
      	for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        }
      
      	return dp[n-1];
    }

	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
      	int n = scanner.nextInt();
      	int k = scanner.nextInt(); scanner.nextLine();
      	int[] arr = new int[n];
      	
      	for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        }
      
      	System.out.println(Main.solve(n,k,arr));
      
      	scanner.close();
    }
} Main static int solve(int n, int k, int[] arr){
    	int[] dp = new int[n];
      	dp[0] = 0;
        dp[1] = Math.abs(arr[0] - arr[1]);
      
      	for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        }
      
      	return dp[n-1];
    } solve {
    	int[] dp = new int[n];
      	dp[0] = 0;
        dp[1] = Math.abs(arr[0] - arr[1]);
      
      	for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        }
      
      	return dp[n-1];
    } int[] dp = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(arr[0] - arr[1]) dp[1] dp 1 Math.abs(arr[0] - arr[1]) Math.abs Math arr[0] - arr[1] arr[0] arr 0 arr[1] arr 1 for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        } int i = 2; int i = 2; i 2 i < n i n i++ i++ i {
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        } {
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        } int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]); min dp[i-1] + Math.abs(arr[i-1] - arr[i]) dp[i-1] dp i-1 i 1 Math.abs(arr[i-1] - arr[i]) Math.abs Math arr[i-1] - arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            } int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            } {
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            } if(i - j < 0)
                      break; i - j < 0 i - j i j 0 break; int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]); h dp[i-j] + Math.abs(arr[i] - arr[i-j]) dp[i-j] dp i-j i j Math.abs(arr[i] - arr[i-j]) Math.abs Math arr[i] - arr[i-j] arr[i] arr i arr[i-j] arr i-j i j min = (h <= min) ? h : min min (h <= min) ? h : min (h <= min) h min h min dp[i] = min dp[i] dp i min return dp[n-1]; dp[n-1] dp n-1 n 1 int n n int k k int[] arr arr public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
      	int n = scanner.nextInt();
      	int k = scanner.nextInt(); scanner.nextLine();
      	int[] arr = new int[n];
      	
      	for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        }
      
      	System.out.println(Main.solve(n,k,arr));
      
      	scanner.close();
    } main {
    	Scanner scanner = new Scanner(System.in);
      	int n = scanner.nextInt();
      	int k = scanner.nextInt(); scanner.nextLine();
      	int[] arr = new int[n];
      	
      	for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        }
      
      	System.out.println(Main.solve(n,k,arr));
      
      	scanner.close();
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner scanner.nextLine() scanner.nextLine scanner int[] arr = new int[n]; arr new int[n] n for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
        	arr[i] = scanner.nextInt();
        } {
        	arr[i] = scanner.nextInt();
        } arr[i] = scanner.nextInt() arr[i] arr i scanner.nextInt() scanner.nextInt scanner System.out.println(Main.solve(n,k,arr)) System.out.println System.out System System.out Main.solve(n,k,arr) Main.solve Main n k arr scanner.close() scanner.close scanner String[] args args
import java.util.Scanner;
import java.lang.Math;

class Main{
  
  	static int solve(int n, int k, int[] arr){
    	int[] dp = new int[n];
      	dp[0] = 0;
        dp[1] = Math.abs(arr[0] - arr[1]);
      
      	for(int i = 2; i < n; i++){
        	int min = dp[i-1] + Math.abs(arr[i-1] - arr[i]);
          
          	for(int j = 1; j <= k; j++){
            	if(i - j < 0)
                      break;

              	int h = dp[i-j] + Math.abs(arr[i] - arr[i-j]);
              	min = (h <= min) ? h : min;
            }
          	dp[i] = min;
        }
      
      	return dp[n-1];
    }

	public static void main(String[] args){
    	Scanner scanner = new Scanner(System.in);
      	int n = scanner.nextInt();
      	int k = scanner.nextInt(); scanner.nextLine();
      	int[] arr = new int[n];
      	
      	for(int i = 0; i < n; i++){
        	arr[i] = scanner.nextInt();
        }
      
      	System.out.println(Main.solve(n,k,arr));
      
      	scanner.close();
    }
}