/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
class Main {
    
    static int[] h;
    static int[] dp;
    static int n, k;
    
    static int solve(int start, int end)
    {
        //base cases
        if(end-start<=0)
			return 0;
		if(end-start==1)
		    return Math.abs(h[end]-h[start]);
		
        
        if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start];
    }
    
	public static void main (String[] args) {
		Scanner scn = new Scanner(System.in);
		
		n = scn.nextInt();
		k = scn.nextInt();
		
		h = new int[n];
		dp = new int[n];
		
		for(int i=0; i<n; i++)
			h[i] = scn.nextInt();
		
		Arrays.fill(dp, -1); 
		
		System.out.println(solve(0, n-1));
	}
} import java.io.*; io.* java import java.util.*; util.* java class Main {
    
    static int[] h;
    static int[] dp;
    static int n, k;
    
    static int solve(int start, int end)
    {
        //base cases
        if(end-start<=0)
			return 0;
		if(end-start==1)
		    return Math.abs(h[end]-h[start]);
		
        
        if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start];
    }
    
	public static void main (String[] args) {
		Scanner scn = new Scanner(System.in);
		
		n = scn.nextInt();
		k = scn.nextInt();
		
		h = new int[n];
		dp = new int[n];
		
		for(int i=0; i<n; i++)
			h[i] = scn.nextInt();
		
		Arrays.fill(dp, -1); 
		
		System.out.println(solve(0, n-1));
	}
} Main static int[] h; h static int[] dp; dp static int n n k; k static int solve(int start, int end)
    {
        //base cases
        if(end-start<=0)
			return 0;
		if(end-start==1)
		    return Math.abs(h[end]-h[start]);
		
        
        if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start];
    } solve {
        //base cases
        if(end-start<=0)
			return 0;
		if(end-start==1)
		    return Math.abs(h[end]-h[start]);
		
        
        if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start];
    } if(end-start<=0)
			return 0; end-start<=0 end-start end start 0 return 0; 0 if(end-start==1)
		    return Math.abs(h[end]-h[start]); end-start==1 end-start end start 1 return Math.abs(h[end]-h[start]); Math.abs(h[end]-h[start]) Math.abs Math h[end]-h[start] h[end] h end h[start] h start if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start]; dp[start]==-1 dp[start] dp start -1 1 {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        } int temp = Integer.MAX_VALUE; temp Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            } int i = 1; int i = 1; i 1 i<=k i k i++ i++ i {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            } {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            } if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i])); start + i<=n - 1 start + i start i n - 1 n 1 temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i])) temp Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i])) Math.min Math temp solve(start + i, end) + Math.abs(h[start] - h[start + i]) solve(start + i, end) solve start + i start i end Math.abs(h[start] - h[start + i]) Math.abs Math h[start] - h[start + i] h[start] h start h[start + i] h start + i start i dp[start] = temp dp[start] dp start temp return temp; temp return dp[start]; dp[start] dp start int start start int end end public static void main (String[] args) {
		Scanner scn = new Scanner(System.in);
		
		n = scn.nextInt();
		k = scn.nextInt();
		
		h = new int[n];
		dp = new int[n];
		
		for(int i=0; i<n; i++)
			h[i] = scn.nextInt();
		
		Arrays.fill(dp, -1); 
		
		System.out.println(solve(0, n-1));
	} main {
		Scanner scn = new Scanner(System.in);
		
		n = scn.nextInt();
		k = scn.nextInt();
		
		h = new int[n];
		dp = new int[n];
		
		for(int i=0; i<n; i++)
			h[i] = scn.nextInt();
		
		Arrays.fill(dp, -1); 
		
		System.out.println(solve(0, n-1));
	} Scanner scn = new Scanner(System.in); scn new Scanner(System.in) n = scn.nextInt() n scn.nextInt() scn.nextInt scn k = scn.nextInt() k scn.nextInt() scn.nextInt scn h = new int[n] h new int[n] n dp = new int[n] dp new int[n] n for(int i=0; i<n; i++)
			h[i] = scn.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i h[i] = scn.nextInt(); h[i] = scn.nextInt() h[i] h i scn.nextInt() scn.nextInt scn Arrays.fill(dp, -1) Arrays.fill Arrays dp -1 1 System.out.println(solve(0, n-1)) System.out.println System.out System System.out solve(0, n-1) solve 0 n-1 n 1 String[] args args
/*package whatever //do not write package name here */

import java.io.*;
import java.util.*;
class Main {
    
    static int[] h;
    static int[] dp;
    static int n, k;
    
    static int solve(int start, int end)
    {
        //base cases
        if(end-start<=0)
			return 0;
		if(end-start==1)
		    return Math.abs(h[end]-h[start]);
		
        
        if(dp[start]==-1)
        {
            int temp = Integer.MAX_VALUE;
            for(int i = 1; i<=k; i++)
            {   
                if(start + i<=n - 1)
                    temp = Math.min(temp, solve(start + i, end) + Math.abs(h[start] - h[start + i]));
            }
            dp[start] = temp;
            return temp;
        }
        else
            return dp[start];
    }
    
	public static void main (String[] args) {
		Scanner scn = new Scanner(System.in);
		
		n = scn.nextInt();
		k = scn.nextInt();
		
		h = new int[n];
		dp = new int[n];
		
		for(int i=0; i<n; i++)
			h[i] = scn.nextInt();
		
		Arrays.fill(dp, -1); 
		
		System.out.println(solve(0, n-1));
	}
}