// Frog 2

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Main {
	public static int frogJumpCost2(int[] arr, int n, int k) {
		int[] dp = new int[n];
		dp[n-1] = 0;
		dp[n-2] = Math.abs(arr[n-1]-arr[n-2]);
		for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		}
		return dp[0];
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		String[] s1 = br.readLine().split(" ");

		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);

		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		}

		System.out.println(frogJumpCost2(arr, n, k));

	}
	public static int min(int...arr) {
		int min = Integer.MAX_VALUE;
		for(int i: arr) {
			if(i < min)
				min = i;
		}
		return min;
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io class Main {
	public static int frogJumpCost2(int[] arr, int n, int k) {
		int[] dp = new int[n];
		dp[n-1] = 0;
		dp[n-2] = Math.abs(arr[n-1]-arr[n-2]);
		for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		}
		return dp[0];
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		String[] s1 = br.readLine().split(" ");

		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);

		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		}

		System.out.println(frogJumpCost2(arr, n, k));

	}
	public static int min(int...arr) {
		int min = Integer.MAX_VALUE;
		for(int i: arr) {
			if(i < min)
				min = i;
		}
		return min;
	}
} Main public static int frogJumpCost2(int[] arr, int n, int k) {
		int[] dp = new int[n];
		dp[n-1] = 0;
		dp[n-2] = Math.abs(arr[n-1]-arr[n-2]);
		for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		}
		return dp[0];
	} frogJumpCost2 {
		int[] dp = new int[n];
		dp[n-1] = 0;
		dp[n-2] = Math.abs(arr[n-1]-arr[n-2]);
		for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		}
		return dp[0];
	} int[] dp = new int[n]; dp new int[n] n dp[n-1] = 0 dp[n-1] dp n-1 n 1 0 dp[n-2] = Math.abs(arr[n-1]-arr[n-2]) dp[n-2] dp n-2 n 2 Math.abs(arr[n-1]-arr[n-2]) Math.abs Math arr[n-1]-arr[n-2] arr[n-1] arr n-1 n 1 arr[n-2] arr n-2 n 2 for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		} int i = n-3; int i = n-3; i n-3 n 3 i >= 0 i 0 i-- i-- i {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		} {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			} int j = 1; int j = 1; j 1 j <= k && (i+j) < n j <= k j k (i+j) < n (i+j) i j n j++ j++ j {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			} {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			} int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]); x dp[i+j]+Math.abs(arr[i]-arr[i+j]) dp[i+j] dp i+j i j Math.abs(arr[i]-arr[i+j]) Math.abs Math arr[i]-arr[i+j] arr[i] arr i arr[i+j] arr i+j i j if(x < min)
					min = x; x < min x min min = x min x dp[i] = min dp[i] dp i min return dp[0]; dp[0] dp 0 int[] arr arr int n n int k k public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		String[] s1 = br.readLine().split(" ");

		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);

		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		}

		System.out.println(frogJumpCost2(arr, n, k));

	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		String[] s1 = br.readLine().split(" ");

		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);

		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		}

		System.out.println(frogJumpCost2(arr, n, k));

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s = br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " String[] s1 = br.readLine().split(" "); s1 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n = Integer.parseInt(s[0]); n Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int k = Integer.parseInt(s[1]); k Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 int[] arr = new int[n]; arr new int[n] n for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			arr[i] = Integer.parseInt(s1[i]);
		} {
			arr[i] = Integer.parseInt(s1[i]);
		} arr[i] = Integer.parseInt(s1[i]) arr[i] arr i Integer.parseInt(s1[i]) Integer.parseInt Integer s1[i] s1 i System.out.println(frogJumpCost2(arr, n, k)) System.out.println System.out System System.out frogJumpCost2(arr, n, k) frogJumpCost2 arr n k String[] args args public static int min(int...arr) {
		int min = Integer.MAX_VALUE;
		for(int i: arr) {
			if(i < min)
				min = i;
		}
		return min;
	} min {
		int min = Integer.MAX_VALUE;
		for(int i: arr) {
			if(i < min)
				min = i;
		}
		return min;
	} int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i: arr) {
			if(i < min)
				min = i;
		} int i arr {
			if(i < min)
				min = i;
		} {
			if(i < min)
				min = i;
		} if(i < min)
				min = i; i < min i min min = i min i return min; min int...arr arr
// Frog 2

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class Main {
	public static int frogJumpCost2(int[] arr, int n, int k) {
		int[] dp = new int[n];
		dp[n-1] = 0;
		dp[n-2] = Math.abs(arr[n-1]-arr[n-2]);
		for(int i = n-3; i >= 0; i--) {
			int min = Integer.MAX_VALUE;
			for(int j = 1; j <= k && (i+j) < n; j++) {
				int x = dp[i+j]+Math.abs(arr[i]-arr[i+j]);
				if(x < min)
					min = x;
			}
			dp[i] = min;
		}
		return dp[0];
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		String[] s1 = br.readLine().split(" ");

		int n = Integer.parseInt(s[0]);
		int k = Integer.parseInt(s[1]);

		int[] arr = new int[n];
		for(int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s1[i]);
		}

		System.out.println(frogJumpCost2(arr, n, k));

	}
	public static int min(int...arr) {
		int min = Integer.MAX_VALUE;
		for(int i: arr) {
			if(i < min)
				min = i;
		}
		return min;
	}
}