
import java.util.*;
import java.io.*;

public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
} Main static FastReader in = new FastReader(); in new FastReader() public static void main(String[] args) {
		solve();
	} main {
		solve();
	} solve() solve String[] args args static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	} solve {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	} int n = in.nextInt(); n in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int[] arr = in.readarr(n); arr in.readarr(n) in.readarr in n 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 = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		} {
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		} for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			} int j = 1; int j = 1; j 1 j <= k j k j++ j++ j {
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			} {
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			} if(i + j >= n) break; i + j >= n i + j i j n break; dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]) dp[i + j] dp i + j i j Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]) Math.min Math dp[i] + Math.abs(arr[i] - arr[i + j]) dp[i] dp i 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 dp[i + j] dp i + j i j System.out.println(dp[n - 1]) System.out.println System.out System System.out dp[n - 1] dp n - 1 n 1 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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	} 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 int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		} readarr {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		} int[] temp = new int[abc]; temp new int[abc] abc for (int i = 0; i < abc; i++)
				temp[i] = nextInt(); int i = 0; int i = 0; i 0 i < abc i abc i++ i++ i temp[i] = nextInt(); temp[i] = nextInt() temp[i] temp i nextInt() nextInt return temp; temp int abc abc public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
} public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
} Main

import java.util.*;
import java.io.*;

public class Main {
	static FastReader in = new FastReader();

	public static void main(String[] args) {
		solve();
	}

	static void solve() {
		int n = in.nextInt();
		int k = in.nextInt();
		int[] arr = in.readarr(n);
		int[] dp = new int[n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 1; j <= k; j++)
			{
				if(i + j >= n) break;
				dp[i + j] = Math.min(dp[i] + Math.abs(arr[i] - arr[i + j]), dp[i + j]);
			}
		}
		System.out.println(dp[n - 1]);
	}

	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;
		}

		int[] readarr(int abc) {
			int[] temp = new int[abc];
			for (int i = 0; i < abc; i++)
				temp[i] = nextInt();
			return temp;
		}
	}
}
