//package compilers;

import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	}

} import java.util.*; util.* java public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	} main {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	} Scanner in=new Scanner(System.in); in new Scanner(System.in) int n=in.nextInt(); n in.nextInt() in.nextInt in int k=in.nextInt(); k in.nextInt() in.nextInt in int []arr=new int[n]; arr new int[n] n int []dp= new int[n]; dp new int[n] n for(int i=0;i<n;i++)
			arr[i]=in.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i]=in.nextInt(); arr[i]=in.nextInt() arr[i] arr i in.nextInt() in.nextInt in 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=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		} {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		} for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			} int j=1; int j=1; j 1 j<=k j k j++ j++ j {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			} {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			} if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				} i-j>=0 i-j i j 0 {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				} dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j])) dp[i] dp i Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j])) Math.min Math dp[i] dp i dp[i-j]+Math.abs(arr[i]-arr[i-j]) dp[i-j] dp i-j i j 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 System.out.println(dp[n-1]) System.out.println System.out System System.out dp[n-1] dp n-1 n 1 String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	}

} Main
//package compilers;

import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		int k=in.nextInt();
		int []arr=new int[n];
		int []dp= new int[n];
		for(int i=0;i<n;i++)
			arr[i]=in.nextInt();
		Arrays.fill(dp,Integer.MAX_VALUE);
		dp[0]=0;
		
		for(int i=1;i<n;i++) {
			for(int j=1;j<=k;j++) {
				if(i-j>=0) {
					dp[i]=Math.min(dp[i],dp[i-j]+Math.abs(arr[i]-arr[i-j]));
				}
			}
		}
		System.out.println(dp[n-1]);
	}

}
