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

public class Main {
	public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	}
} import java.io.*; io.* java import java.lang.*; lang.* java public class Main {
	public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	}
} Main public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	} main {
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	} BufferedReader br = new BufferedReader( new InputStreamReader ( System.in)); br new BufferedReader( new InputStreamReader ( System.in)) String line1= br.readLine(); line1 br.readLine() br.readLine br String strs1[]= line1.trim().split("\\s+"); strs1 line1.trim().split("\\s+") line1.trim().split line1.trim() line1.trim line1 "\\s+" int n= Integer.parseInt( strs1[0] ); n Integer.parseInt( strs1[0] ) Integer.parseInt Integer strs1[0] strs1 0 int k= Integer.parseInt(strs1[1]); k Integer.parseInt(strs1[1]) Integer.parseInt Integer strs1[1] strs1 1 int arr[]= new int[n]; arr new int[n] n String line=br.readLine(); line br.readLine() br.readLine br String strs[]=line.trim().split("\\s+"); strs line.trim().split("\\s+") line.trim().split line.trim() line.trim line "\\s+" for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i]=Integer.parseInt(strs[i]); arr[i]=Integer.parseInt(strs[i]) arr[i] arr i Integer.parseInt(strs[i]) Integer.parseInt Integer strs[i] strs i int calc[]=new int[n]; calc new int[n] n calc[0]=0 calc[0] calc 0 0 for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   } int i=1; int i=1; i 1 i<n i n i++ i++ i {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   } {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   } int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] ); m calc[i-1] + Math.abs(arr[i]-arr[i-1] ) calc[i-1] calc i-1 i 1 Math.abs(arr[i]-arr[i-1] ) Math.abs Math arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 int count=1; count 1 for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       } int j=i-2 ; int j=i-2 ; j i-2 i 2 j>=0 && count<k j>=0 j 0 count<k count k j-- j-- j {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       } {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       } int comp=calc[j] + Math.abs(arr[j]-arr[i]); comp calc[j] + Math.abs(arr[j]-arr[i]) calc[j] calc j Math.abs(arr[j]-arr[i]) Math.abs Math arr[j]-arr[i] arr[j] arr j arr[i] arr i count+=1 count 1 m=Math.min(m , comp) m Math.min(m , comp) Math.min Math m comp calc[i]=m calc[i] calc i m System.out.println( calc[n-1]) System.out.println System.out System System.out calc[n-1] calc n-1 n 1 String[] args args public class Main {
	public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	}
} public class Main {
	public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	}
} Main
import java.io.*;
import java.lang.*;

public class Main {
	public static void main (String[] args) throws IOException
	{
	   BufferedReader br = new BufferedReader( new InputStreamReader ( System.in));
	   String line1= br.readLine();
	   String strs1[]= line1.trim().split("\\s+");
	   int n= Integer.parseInt( strs1[0] );
	   int k= Integer.parseInt(strs1[1]);
	   
	   int arr[]= new int[n];
	   
	   String line=br.readLine();
	   String strs[]=line.trim().split("\\s+");
	   
	   for(int i=0; i<n ;i++)
	   arr[i]=Integer.parseInt(strs[i]);
	   
	   int calc[]=new int[n];
	   calc[0]=0;
	   
	   for(int i=1;i<n ; i++)
	   {
	       int m= calc[i-1] + Math.abs(arr[i]-arr[i-1] );
	       int count=1;
	       for(int j=i-2 ; j>=0 && count<k ; j--)
	       {
	           int comp=calc[j] + Math.abs(arr[j]-arr[i]);
	           count+=1;
	           m=Math.min(m , comp);
	       }
	       
	       calc[i]=m;

	   }
	   System.out.println( calc[n-1]);
		//code
	}
}