import java.io.*;
import java.math.*;
import java.util.*;
import java.lang.*;

// import java.text.DecimalFormat; 
// import java.text.DecimalFormatSymbols; 
 
 
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
 
public class Main {
    
    
    public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    }
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	}
   
} import java.io.*; io.* java import java.math.*; math.* java import java.util.*; util.* java import java.lang.*; lang.* java public class Main {
    
    
    public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    }
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	}
   
} Main public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    } main {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    } StringBuffer str = new StringBuffer(); str new StringBuffer() PrintWriter pw=new PrintWriter(System.out); pw new PrintWriter(System.out) FastScanner sc = new FastScanner(); sc new FastScanner() int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            a[i] = sc.nextInt();
        } {
            a[i] = sc.nextInt();
        } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc long[] dp = new long[n]; dp new long[n] n for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        } {
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        } long min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int in = Math.max(0,i-k); in Math.max(0,i-k) Math.max Math 0 i-k i k for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            } int j=i-1; int j=i-1; j i-1 i 1 (j>=in) j in j-- j-- j {
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            } {
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            } if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]); min>dp[j]+Math.abs(a[i]-a[j]) min dp[j]+Math.abs(a[i]-a[j]) dp[j] dp j Math.abs(a[i]-a[j]) Math.abs Math a[i]-a[j] a[i] a i a[j] a j min=dp[j]+Math.abs(a[i]-a[j]) min dp[j]+Math.abs(a[i]-a[j]) dp[j] dp j Math.abs(a[i]-a[j]) Math.abs Math a[i]-a[j] a[i] a i a[j] a j dp[i] = min dp[i] dp i min str.append(dp[n-1]) str.append str dp[n-1] dp n-1 n 1 pw.println(str.toString()) pw.println pw str.toString() str.toString str pw.close() pw.close pw String args[] args static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	} FastScanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(""); st new StringTokenizer("") public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} next {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st 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 public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		} next {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		} try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} IOException e {
				e.printStackTrace();
			} e.printStackTrace() e.printStackTrace e {
				return br.readLine();
			} return br.readLine(); br.readLine() br.readLine br return "Not able to read"; "Not able to read" int k k 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 public class Main {
    
    
    public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    }
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	}
   
} public class Main {
    
    
    public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    }
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	}
   
} Main
import java.io.*;
import java.math.*;
import java.util.*;
import java.lang.*;

// import java.text.DecimalFormat; 
// import java.text.DecimalFormatSymbols; 
 
 
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
 
public class Main {
    
    
    public static void main(String args[] ) throws Exception {
        StringBuffer str = new StringBuffer();
        PrintWriter pw=new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i] = sc.nextInt();
        }
        long[] dp = new long[n];
        for(int i=1;i<n;i++){
            long min = Integer.MAX_VALUE;
            int in = Math.max(0,i-k);
            for(int j=i-1;(j>=in);j--){
                if(min>dp[j]+Math.abs(a[i]-a[j]))
                    min=dp[j]+Math.abs(a[i]-a[j]);
            }
            dp[i] = min;
        }
        
        str.append(dp[n-1]);
        
        pw.println(str.toString());
        
        pw.close();
    }
     
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		
		public String next() {
			while (!st.hasMoreElements())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		public String next(int k) {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "Not able to read";
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		
		long nextLong() {
			return Long.parseLong(next());
		}
		
		double nextDouble() {
			return Double.parseDouble(next());
		}
		
	    
	}
   
}