/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	}
} Main public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	} main {
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	} BufferedReader br=  new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] in = br.readLine().split(" "); in br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n = Integer.parseInt(in[0]); n Integer.parseInt(in[0]) Integer.parseInt Integer in[0] in 0 int k = Integer.parseInt(in[1]); k Integer.parseInt(in[1]) Integer.parseInt Integer in[1] in 1 in = br.readLine().split(" ") in br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            arr[i] = Integer.parseInt(in[i]);
        } {
            arr[i] = Integer.parseInt(in[i]);
        } arr[i] = Integer.parseInt(in[i]) arr[i] arr i Integer.parseInt(in[i]) Integer.parseInt Integer in[i] in i int[] dp = new int[n]; dp new int[n] n dp[0] = 0 dp[0] dp 0 0 dp[1] = dp[0]+Math.abs(arr[0]-arr[1]) dp[1] dp 1 dp[0]+Math.abs(arr[0]-arr[1]) dp[0] dp 0 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++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        } int i=2; int i=2; i 2 i<n i n i++ i++ i {
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        } {
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        } dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE int curr = i; curr i for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            } int j=1; int j=1; j 1 j<=k && curr>=1 j<=k j k curr>=1 curr 1 j++,curr-- j++ j curr-- curr {
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            } {
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            } dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]) dp[i] dp i Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]) Math.min Math dp[i-j]+Math.abs(arr[i-j]-arr[i]) dp[i-j] dp i-j i j Math.abs(arr[i-j]-arr[i]) Math.abs Math arr[i-j]-arr[i] arr[i-j] arr i-j i j arr[i] arr i dp[i] dp i 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 public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	}
} Main
/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br=  new BufferedReader(new InputStreamReader(System.in));
		
		String[] in = br.readLine().split(" ");
		int n = Integer.parseInt(in[0]);
		int k = Integer.parseInt(in[1]);
		
// 		StringBuilder str = new StringBuilder();
		
		in = br.readLine().split(" ");
		int[] arr = new int[n];

        for(int i=0;i<n;i++){
            arr[i] = Integer.parseInt(in[i]);
        }
        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = dp[0]+Math.abs(arr[0]-arr[1]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            int curr = i;
            for(int j=1;j<=k && curr>=1;j++,curr--){
                dp[i] = Math.min(dp[i-j]+Math.abs(arr[i-j]-arr[i]),dp[i]);
            }
        }
        System.out.println(dp[n-1]);
	}
}
