//package cp;
import java.util.*;
import java.io.*;
public class Main {
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	}
}
class Readers {
 
    static BufferedReader reader;
 
    static StringTokenizer tokenizer;
 
 
 
    /** call this method to initialize reader for InputStream */
 
    static void init(InputStream input) {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    }
 
 
 
    /** get next word */
 
    static String next() throws IOException {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    }
 
 
 
    static int nextInt() throws IOException {
 
        return Integer.parseInt( next() );
 
    }
 
	
 
    static double nextDouble() throws IOException {
 
        return Double.parseDouble( next() );
 
    }
    
    
    static long nextLong() throws IOException{
    	
		return Long.parseLong(next());
	}
 
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	}
} Main StringBuilder sb=new StringBuilder(); sb new StringBuilder() Integer[] ARR=new Integer[5]; ARR new Integer[5] 5 long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);} gcd {if(b==0)return a;else return gcd(b,a%b);} if(b==0)return a;else return gcd(b,a%b); b==0 b 0 return a; a return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b long a a long b b void swap(long a,long b) {long temp=a;a=b;b=temp;} swap {long temp=a;a=b;b=temp;} long temp=a; temp a a=b a b b=temp b temp long a a long b b //Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}} divisor {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}} int cnt=0; cnt 0 for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}} int i=start; int i=start; i start i<=Math.sqrt(n) i Math.sqrt(n) Math.sqrt Math n i++ i++ i {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}} {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}} if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;} n%i==0 n%i n i 0 {if(i==n/i)cnt++;else cnt+=2;} if(i==n/i)cnt++;else cnt+=2; i==n/i i n/i n i cnt++ cnt cnt+=2 cnt 2 long n n int start start public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	} main {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	} PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) Readers.init(System.in) Readers.init Readers System.in System System.in Main obj=new Main(); obj new Main() int n=Readers.nextInt(); n Readers.nextInt() Readers.nextInt Readers int k=Readers.nextInt(); k Readers.nextInt() Readers.nextInt Readers long[] a=new long[n+1]; a new long[n+1] n+1 n 1 for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		} int i = 1; int i = 1; i 1 i < a.length i a.length a a.length i++ i++ i {
			a[i]=Readers.nextLong();
		} {
			a[i]=Readers.nextLong();
		} a[i]=Readers.nextLong() a[i] a i Readers.nextLong() Readers.nextLong Readers long[] dp=new long[n+1]; dp new long[n+1] n+1 n 1 dp[1]=0 dp[1] dp 1 0 for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		} int i = 2; int i = 2; i 2 i <=n i n i++ i++ i {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		} {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		} long minn=Integer.MAX_VALUE; minn Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			} int j = 1; int j = 1; j 1 j <=k j k j++ j++ j {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			} {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			} if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j])); (i-j)>=1 (i-j) i j 1 minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j])) minn Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j])) Math.min Math minn dp[i-j]+Math.abs(a[i]-a[i-j]) dp[i-j] dp i-j i j Math.abs(a[i]-a[i-j]) Math.abs Math a[i]-a[i-j] a[i] a i a[i-j] a i-j i j dp[i]=minn dp[i] dp i minn System.out.println(dp[n]) System.out.println System.out System System.out dp[n] dp n out.flush() out.flush out String[] args args class Readers {
 
    static BufferedReader reader;
 
    static StringTokenizer tokenizer;
 
 
 
    /** call this method to initialize reader for InputStream */
 
    static void init(InputStream input) {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    }
 
 
 
    /** get next word */
 
    static String next() throws IOException {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    }
 
 
 
    static int nextInt() throws IOException {
 
        return Integer.parseInt( next() );
 
    }
 
	
 
    static double nextDouble() throws IOException {
 
        return Double.parseDouble( next() );
 
    }
    
    
    static long nextLong() throws IOException{
    	
		return Long.parseLong(next());
	}
 
} Readers static BufferedReader reader; reader static StringTokenizer tokenizer; tokenizer /** call this method to initialize reader for InputStream */
 
    static void init(InputStream input) {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    } init {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    } reader = new BufferedReader(
 
                     new InputStreamReader(input) ) reader new BufferedReader(
 
                     new InputStreamReader(input) ) tokenizer = new StringTokenizer("") tokenizer new StringTokenizer("") InputStream input input /** get next word */
 
    static String next() throws IOException {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    } next {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    } while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        } ! tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        } tokenizer = new StringTokenizer(
 
                   reader.readLine() ) tokenizer new StringTokenizer(
 
                   reader.readLine() ) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer static int nextInt() throws IOException {
 
        return Integer.parseInt( next() );
 
    } nextInt {
 
        return Integer.parseInt( next() );
 
    } return Integer.parseInt( next() ); Integer.parseInt( next() ) Integer.parseInt Integer next() next static double nextDouble() throws IOException {
 
        return Double.parseDouble( next() );
 
    } nextDouble {
 
        return Double.parseDouble( next() );
 
    } return Double.parseDouble( next() ); Double.parseDouble( next() ) Double.parseDouble Double next() next static long nextLong() throws IOException{
    	
		return Long.parseLong(next());
	} nextLong {
    	
		return Long.parseLong(next());
	} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	}
} public class Main {
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	}
} Main
//package cp;
import java.util.*;
import java.io.*;
public class Main {
	long gcd(long a,long b) {if(b==0)return a;else return gcd(b,a%b);}
	void swap(long a,long b) {long temp=a;a=b;b=temp;}
	StringBuilder sb=new StringBuilder();
	Integer[] ARR=new Integer[5];
	//Integer sort-TLE-Initialize object elements i.e a[0].
	void divisor(long n,int start) {
		int cnt=0;for(int i=start;i<=Math.sqrt(n);i++) {
		if(n%i==0) {if(i==n/i)cnt++;else cnt+=2;}}}
	
	public static void main(String[] args) throws IOException {	
		PrintWriter out = new PrintWriter(System.out);
		Readers.init(System.in);
		Main obj=new Main();
		int n=Readers.nextInt();
		int k=Readers.nextInt();
		long[] a=new long[n+1];
		for (int i = 1; i < a.length; i++) {
			a[i]=Readers.nextLong();
		}
		long[] dp=new long[n+1];
		dp[1]=0;
		for (int i = 2; i <=n; i++) {
			long minn=Integer.MAX_VALUE;
			for (int j = 1; j <=k ; j++) {
				if((i-j)>=1)minn=Math.min(minn, dp[i-j]+Math.abs(a[i]-a[i-j]));
			}
			dp[i]=minn;
		}
//		dp[2]=Math.abs(a[2]-a[1]);
//		for (int i = 3; i <=n; i++) {
//			long minn=Math.min(dp[i-1]+Math.abs(a[i]-a[i-1]),dp[i-2]+Math.abs(a[i]-a[i-2]));
//			dp[i]=minn;
//		}
		System.out.println(dp[n]);
		out.flush();
	}
}
class Readers {
 
    static BufferedReader reader;
 
    static StringTokenizer tokenizer;
 
 
 
    /** call this method to initialize reader for InputStream */
 
    static void init(InputStream input) {
 
        reader = new BufferedReader(
 
                     new InputStreamReader(input) );
 
        tokenizer = new StringTokenizer("");
 
    }
 
 
 
    /** get next word */
 
    static String next() throws IOException {
 
        while ( ! tokenizer.hasMoreTokens() ) {
 
            //TODO add check for eof if necessary
 
            tokenizer = new StringTokenizer(
 
                   reader.readLine() );
 
        }
 
        return tokenizer.nextToken();
 
    }
 
 
 
    static int nextInt() throws IOException {
 
        return Integer.parseInt( next() );
 
    }
 
	
 
    static double nextDouble() throws IOException {
 
        return Double.parseDouble( next() );
 
    }
    
    
    static long nextLong() throws IOException{
    	
		return Long.parseLong(next());
	}
 
}
