import java.util.Collections;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
}

class Reader { 
	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tok = new StringTokenizer("");
	static String next() {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} 
	static int nextInt() {
		return Integer.parseInt(next());
	}
} import java.util.Collections; Collections java.util import java.util.StringTokenizer; StringTokenizer java.util import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
} Main public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	} main {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	} Reader rd = new Reader(); rd new Reader() int n = rd.nextInt(); n rd.nextInt() rd.nextInt rd int k = rd.nextInt(); k rd.nextInt() rd.nextInt rd int[] h = new int[n+1]; h new int[n+1] n+1 n 1 h[0] = 0 h[0] h 0 0 for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		} int nn=1; int nn=1; nn 1 nn<n+1 nn n+1 n 1 nn++ nn++ nn {
			h[nn] = rd.nextInt();
		} {
			h[nn] = rd.nextInt();
		} h[nn] = rd.nextInt() h[nn] h nn rd.nextInt() rd.nextInt rd int[] dp = new int[n+1]; dp new int[n+1] n+1 n 1 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 dp[1] = 0 dp[1] dp 1 0 dp[2] = Math.abs(h[2]-h[1]) dp[2] dp 2 Math.abs(h[2]-h[1]) Math.abs Math h[2]-h[1] h[2] h 2 h[1] h 1 for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		} int nn=3; int nn=3; nn 3 nn<=n nn n nn++ nn++ nn {
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		} {
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		} int val = 1; val 1 while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			} val <= k && nn-val >= 1 val <= k val k nn-val >= 1 nn-val nn val 1 {
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			} dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]) dp[nn] dp nn Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]) Math.min Math dp[nn] dp nn Math.abs(h[nn]-h[nn-val]) + dp[nn-val] Math.abs(h[nn]-h[nn-val]) Math.abs Math h[nn]-h[nn-val] h[nn] h nn h[nn-val] h nn-val nn val dp[nn-val] dp nn-val nn val val++ val System.out.println(dp[n]) System.out.println System.out System System.out dp[n] dp n String[] args args static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	} sort {
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	} ArrayList<Integer> ls = new ArrayList<>(); ls new ArrayList<>() for(int i=0; i<ar.length; i++) ls.add(ar[i]); int i=0; int i=0; i 0 i<ar.length i ar.length ar ar.length i++ i++ i ls.add(ar[i]); ls.add(ar[i]) ls.add ls ar[i] ar i Collections.sort(ls) Collections.sort Collections ls for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i); int i =0; int i =0; i 0 i<ls.size() i ls.size() ls.size ls i++ i++ i ar[i] = ls.get(i); ar[i] = ls.get(i) ar[i] ar i ls.get(i) ls.get ls i int[] ar ar class Reader { 
	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tok = new StringTokenizer("");
	static String next() {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} 
	static int nextInt() {
		return Integer.parseInt(next());
	}
} Reader static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); bf new BufferedReader(new InputStreamReader(System.in)) static StringTokenizer tok = new StringTokenizer(""); tok new StringTokenizer("") static String next() {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} next {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		} !tok.hasMoreTokens() tok.hasMoreTokens() tok.hasMoreTokens tok {
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		} try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			} catch(IOException e) {
				e.printStackTrace();
			} IOException e {
				e.printStackTrace();
			} e.printStackTrace() e.printStackTrace e {
				tok = new StringTokenizer(bf.readLine());
			} tok = new StringTokenizer(bf.readLine()) tok new StringTokenizer(bf.readLine()) return tok.nextToken(); tok.nextToken() tok.nextToken tok static int nextInt() {
		return Integer.parseInt(next());
	} nextInt {
		return Integer.parseInt(next());
	} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
} public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
} Main
import java.util.Collections;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class Main{
	public static void main(String[] args) {
		Reader rd = new Reader();
		int n = rd.nextInt();
		int k = rd.nextInt();
		int[] h = new int[n+1];
		h[0] = 0;
		for(int nn=1; nn<n+1; nn++){
			h[nn] = rd.nextInt();
		}
		int[] dp = new int[n+1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0] = 0;
		dp[1] = 0;
		dp[2] = Math.abs(h[2]-h[1]);
		for(int nn=3; nn<=n;nn++){
			int val = 1;
			while(val <= k && nn-val >= 1){
				dp[nn] = Math.min(dp[nn], Math.abs(h[nn]-h[nn-val]) + dp[nn-val]);
				val++;
			//System.out.println(Arrays.toString(dp) + " " + val);
			}
			//System.out.println(Arrays.toString(dp));
		}
		System.out.println(dp[n]);
	}

	static void sort(int[] ar){
		ArrayList<Integer> ls = new ArrayList<>();
		for(int i=0; i<ar.length; i++) ls.add(ar[i]);
		Collections.sort(ls);
		for(int i =0; i<ls.size(); i++) ar[i] = ls.get(i);
	}
}

class Reader { 
	static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer tok = new StringTokenizer("");
	static String next() {
		while(!tok.hasMoreTokens()){
			try{
				tok = new StringTokenizer(bf.readLine());
			}
			catch(IOException e) {
				e.printStackTrace();
			}
		}
		return tok.nextToken();
	} 
	static int nextInt() {
		return Integer.parseInt(next());
	}
}
