import java.util.*;
import java.io.*;
 
class Main{
public static void main(String[] args) throws IOException{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	String[] str = br.readLine().split(" ");
  	int n = Integer.parseInt(str[0]);
  	int k = Integer.parseInt(str[1]);
  	int[] h = new int[n];
  	String[] s = br.readLine().split(" ");
  	for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]);
  	int[] dp = new int[n];
    Arrays.fill(dp,Integer.MAX_VALUE);
    dp[0] = 0;
  	//dp[1] = Math.abs(h[1]-h[0]);
  	for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    }
      System.out.println(dp[n-1]);
    }
} import java.util.*; util.* java import java.io.*; io.* java class Main{
public static void main(String[] args) throws IOException{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	String[] str = br.readLine().split(" ");
  	int n = Integer.parseInt(str[0]);
  	int k = Integer.parseInt(str[1]);
  	int[] h = new int[n];
  	String[] s = br.readLine().split(" ");
  	for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]);
  	int[] dp = new int[n];
    Arrays.fill(dp,Integer.MAX_VALUE);
    dp[0] = 0;
  	//dp[1] = Math.abs(h[1]-h[0]);
  	for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    }
      System.out.println(dp[n-1]);
    }
} Main public static void main(String[] args) throws IOException{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	String[] str = br.readLine().split(" ");
  	int n = Integer.parseInt(str[0]);
  	int k = Integer.parseInt(str[1]);
  	int[] h = new int[n];
  	String[] s = br.readLine().split(" ");
  	for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]);
  	int[] dp = new int[n];
    Arrays.fill(dp,Integer.MAX_VALUE);
    dp[0] = 0;
  	//dp[1] = Math.abs(h[1]-h[0]);
  	for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    }
      System.out.println(dp[n-1]);
    } main {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	String[] str = br.readLine().split(" ");
  	int n = Integer.parseInt(str[0]);
  	int k = Integer.parseInt(str[1]);
  	int[] h = new int[n];
  	String[] s = br.readLine().split(" ");
  	for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]);
  	int[] dp = new int[n];
    Arrays.fill(dp,Integer.MAX_VALUE);
    dp[0] = 0;
  	//dp[1] = Math.abs(h[1]-h[0]);
  	for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    }
      System.out.println(dp[n-1]);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] str = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n = Integer.parseInt(str[0]); n Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int k = Integer.parseInt(str[1]); k Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int[] h = new int[n]; h new int[n] n String[] s = br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]); int i=0; int i=0; i 0 i<n i n i++ i++ i h[i] = Integer.parseInt(s[i]); h[i] = Integer.parseInt(s[i]) h[i] h i Integer.parseInt(s[i]) Integer.parseInt Integer s[i] s i int[] dp = new int[n]; dp new int[n] n Arrays.fill(dp,Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    } int i=1; int i=1; i 1 i<n i n i++ i++ i {
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    } {
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    } for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j])); int j=i-1; int j=i-1; j i-1 i 1 j>=0 && i-j<=k j>=0 j 0 i-j<=k i-j i j k j-- j-- j dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j])); dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j])) dp[i] dp i Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j])) Math.min Math dp[i] dp i dp[j] + Math.abs(h[i]-h[j]) dp[j] dp j Math.abs(h[i]-h[j]) Math.abs Math h[i]-h[j] h[i] h i h[j] h j 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
import java.util.*;
import java.io.*;
 
class Main{
public static void main(String[] args) throws IOException{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  	String[] str = br.readLine().split(" ");
  	int n = Integer.parseInt(str[0]);
  	int k = Integer.parseInt(str[1]);
  	int[] h = new int[n];
  	String[] s = br.readLine().split(" ");
  	for(int i=0;i<n;i++)
      h[i] = Integer.parseInt(s[i]);
  	int[] dp = new int[n];
    Arrays.fill(dp,Integer.MAX_VALUE);
    dp[0] = 0;
  	//dp[1] = Math.abs(h[1]-h[0]);
  	for(int i=1;i<n;i++){
      for(int j=i-1;j>=0 && i-j<=k;j--)
      dp[i] = Math.min(dp[i],dp[j] + Math.abs(h[i]-h[j]));
    }
      System.out.println(dp[n-1]);
    }
}
