import java.util.*;
import java.lang.*;
import java.io.*;
 
public class Main
{
	public static void main (String[] args) throws java.lang.Exception{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	}
 
	public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	}
} 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{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	}
 
	public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	}
} Main public static void main (String[] args) throws java.lang.Exception{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	} main {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	} Scanner scn = new Scanner(System.in); scn new Scanner(System.in) int n = scn.nextInt(); n scn.nextInt() scn.nextInt scn int k = scn.nextInt(); k scn.nextInt() scn.nextInt scn int[] arr = new int[n + 1]; arr new int[n + 1] n + 1 n 1 for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int i = 1; int i = 1; i 1 i <= n i n i++ i++ i {
			arr[i] = scn.nextInt();
		} {
			arr[i] = scn.nextInt();
		} arr[i] = scn.nextInt() arr[i] arr i scn.nextInt() scn.nextInt scn int[] memo = new int[n + 1]; memo new int[n + 1] n + 1 n 1 Arrays.fill(memo, -1) Arrays.fill Arrays memo -1 1 System.out.println(mincostKjump_memoized(1, arr, k, memo)) System.out.println System.out System System.out mincostKjump_memoized(1, arr, k, memo) mincostKjump_memoized 1 arr k memo String[] args args public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	} mincostKjump_memoized {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	} if (idx == heights.length - 1)return memo[idx] = 0; idx == heights.length - 1 idx heights.length - 1 heights.length heights heights.length 1 return memo[idx] = 0; memo[idx] = 0 memo[idx] memo idx 0 if (memo[idx] != -1)return memo[idx]; memo[idx] != -1 memo[idx] memo idx -1 1 return memo[idx]; memo[idx] memo idx int ans = Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		} int i = 1; int i = 1; i 1 i <= k && idx + i < heights.length i <= k i k idx + i < heights.length idx + i idx i heights.length heights heights.length i++ i++ i {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		} {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		} int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]); cba mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]) mincostKjump_memoized(idx + i, heights, k, memo) mincostKjump_memoized idx + i idx i heights k memo Math.abs(heights[idx + i] - heights[idx]) Math.abs Math heights[idx + i] - heights[idx] heights[idx + i] heights idx + i idx i heights[idx] heights idx ans = Math.min(ans, cba) ans Math.min(ans, cba) Math.min Math ans cba return memo[idx] = ans; memo[idx] = ans memo[idx] memo idx ans int idx idx int[] heights heights int k k int[] memo memo public class Main
{
	public static void main (String[] args) throws java.lang.Exception{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	}
 
	public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	}
 
	public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	}
} Main
import java.util.*;
import java.lang.*;
import java.io.*;
 
public class Main
{
	public static void main (String[] args) throws java.lang.Exception{
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int k = scn.nextInt();
		int[] arr = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			arr[i] = scn.nextInt();
		} int[] memo = new int[n + 1];
		Arrays.fill(memo, -1);
 
		System.out.println(mincostKjump_memoized(1, arr, k, memo));
	}
 
	public static int mincostKjump_memoized(int idx, int[] heights, int k, int[] memo) {
		if (idx == heights.length - 1)return memo[idx] = 0;
		if (memo[idx] != -1)return memo[idx];
		int ans = Integer.MAX_VALUE;
		for (int i = 1; i <= k && idx + i < heights.length; i++) {
			int cba = mincostKjump_memoized(idx + i, heights, k, memo) + Math.abs(heights[idx + i] - heights[idx]);
			ans = Math.min(ans, cba);
		}
		return memo[idx] = ans;
	}
}