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

public class Main {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	private static long dp[];

	private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	}

	private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	}

	public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	private static long dp[];

	private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	}

	private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	}

	public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	}

} Main private static long dp[]; dp private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	} util {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	} if (start >= end - 1)
			return 0; start >= end - 1 start end - 1 end 1 return 0; 0 if (dp[start] != 0)
			return dp[start]; dp[start] != 0 dp[start] dp start 0 return dp[start]; dp[start] dp start long ans = Long.MAX_VALUE; ans Long.MAX_VALUE Long Long.MAX_VALUE for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		} {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		} if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			} start + j < end start + j start j end {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			} ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end)) ans Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end)) Math.min Math ans Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end) Math.abs(arr[start] - arr[start + j]) Math.abs Math arr[start] - arr[start + j] arr[start] arr start arr[start + j] arr start + j start j util(arr, start + j, k, end) util arr start + j start j k end return dp[start] = ans; dp[start] = ans dp[start] dp start ans int arr[] arr int start start int k k int end end private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	} util1 {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	} dp[0] = 0 dp[0] dp 0 0 dp[1] = Math.abs(arr[1] - arr[0]) dp[1] dp 1 Math.abs(arr[1] - arr[0]) Math.abs Math arr[1] - arr[0] arr[1] arr 1 arr[0] arr 0 for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		} int i=2; int i=2; i 2 i<arr.length i arr.length arr arr.length i++ i++ i {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		} {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		} dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			} int j = i - 1; int j = i - 1; j i - 1 i 1 j >= 0 && j >=i - k j >= 0 j 0 j >=i - k j i - k i k j-- j-- j {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			} {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			} dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] ) dp[i] dp i Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] ) Math.min Math dp[j] + Math.abs(arr[i] - arr[j]) dp[j] dp j Math.abs(arr[i] - arr[j]) Math.abs Math arr[i] - arr[j] arr[i] arr i arr[j] arr j dp[i] dp i return dp[end-1]; dp[end-1] dp end-1 end 1 int arr[] arr int k k int end end public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	} main {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	} FastReader sc = new FastReader(); sc new FastReader() int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int arr[] = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			arr[i] = sc.nextInt();
		} {
			arr[i] = sc.nextInt();
		} arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc dp = new long[n] dp new long[n] n System.out.println(util1(arr, k, n)) System.out.println System.out System System.out util1(arr, k, n) util1 arr k n String[] args args static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	} FastReader BufferedReader br; br StringTokenizer st; st public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		} FastReader {
			br = new BufferedReader(new InputStreamReader(System.in));
		} br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} next {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			} try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st = new StringTokenizer(br.readLine());
				} st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
			return Double.parseDouble(next());
		} nextDouble {
			return Double.parseDouble(next());
		} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		} nextLine {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		} String str = ""; str "" try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} IOException e {
				e.printStackTrace();
			} e.printStackTrace() e.printStackTrace e {
				str = br.readLine();
			} str = br.readLine() str br.readLine() br.readLine br return str; str public class Main {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	private static long dp[];

	private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	}

	private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	}

	public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	}

} public class Main {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	private static long dp[];

	private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	}

	private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	}

	public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}

	private static long dp[];

	private static long util(int arr[], int start, int k, int end) {

		if (start >= end - 1)
			return 0;

		if (dp[start] != 0)
			return dp[start];
		long ans = Long.MAX_VALUE;

		for (int j = 1; j <= k; j++) {

			if (start + j < end) {
				ans = Math.min(ans, Math.abs(arr[start] - arr[start + j]) + util(arr, start + j, k, end));
			}
		}
		return dp[start] = ans;

	}

	private static long util1(int arr[],int k,int end) {
		
		dp[0] = 0;
		dp[1] = Math.abs(arr[1] - arr[0]);
		
		for(int i=2; i<arr.length; i++) {
			dp[i] = Integer.MAX_VALUE;
			for(int j = i - 1;  j >= 0 && j >=i - k;  j--  ) {
			
				dp[i] = Math.min(dp[j] + Math.abs(arr[i] - arr[j]) ,dp[i] );
				
			}
		}
		return dp[end-1];
		
	}

	public static void main(String[] args) {

		FastReader sc = new FastReader();

		int n = sc.nextInt();
		int k = sc.nextInt();

		int arr[] = new int[n];

		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		dp = new long[n];
		System.out.println(util1(arr, k, n));

	}

}
