import java.util.*;


public class Main{
	static int[] dp=new int[1000001];
	
	static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	}
	
	
	
	public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	}

} import java.util.*; util.* java public class Main{
	static int[] dp=new int[1000001];
	
	static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	}
	
	
	
	public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	}

} Main static int[] dp=new int[1000001]; dp new int[1000001] 1000001 static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	} solve {
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	} if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		} i==n-1 i n-1 n 1 {
			return Math.abs(arr[i]-arr[n-1]);
		} return Math.abs(arr[i]-arr[n-1]); Math.abs(arr[i]-arr[n-1]) Math.abs Math arr[i]-arr[n-1] arr[i] arr i arr[n-1] arr n-1 n 1 if(dp[i]!=-1)
		{
			return dp[i];
		} dp[i]!=-1 dp[i] dp i -1 1 {
			return dp[i];
		} return dp[i]; dp[i] dp i int ans=Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		} int it=1; int it=1; it 1 it<=k it k it++ it++ it {
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		} {
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		} if(i+it>=n)
			{
				break;
			} i+it>=n i+it i it n {
				break;
			} break; ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i])) ans Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i])) Math.min Math ans solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]) solve(arr,n,k,i+it) solve arr n k i+it i it Math.abs(arr[i+it]-arr[i]) Math.abs Math arr[i+it]-arr[i] arr[i+it] arr i+it i it arr[i] arr i return dp[i]=ans; dp[i]=ans dp[i] dp i ans int[] arr arr int n n int k k int i i public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	} main {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	} Arrays.fill(dp,0,1000000,-1) Arrays.fill Arrays dp 0 1000000 -1 1 Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int n n k; k n=sc.nextInt() n sc.nextInt() sc.nextInt sc k=sc.nextInt() k sc.nextInt() sc.nextInt sc int[] arr=new int[n]; arr new int[n] n for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i]=sc.nextInt();
		} {
			arr[i]=sc.nextInt();
		} arr[i]=sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc System.out.println(solve(arr, n, k, 0)) System.out.println System.out System System.out solve(arr, n, k, 0) solve arr n k 0 sc.close() sc.close sc String[] args args public class Main{
	static int[] dp=new int[1000001];
	
	static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	}
	
	
	
	public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	}

} public class Main{
	static int[] dp=new int[1000001];
	
	static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	}
	
	
	
	public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	}

} Main
import java.util.*;


public class Main{
	static int[] dp=new int[1000001];
	
	static int solve(int[] arr,int n,int k,int i)
	{
		if(i==n-1)
		{
			return Math.abs(arr[i]-arr[n-1]);
		}
		if(dp[i]!=-1)
		{
			return dp[i];
		}
		int ans=Integer.MAX_VALUE;
		for(int it=1;it<=k;it++)
		{
			if(i+it>=n)
			{
				break;
			}
			ans=Math.min(ans,solve(arr,n,k,i+it)+Math.abs(arr[i+it]-arr[i]));
			
		}
		return dp[i]=ans;
		
	}
	
	
	
	public static void main(String[] args) {
		Arrays.fill(dp,0,1000000,-1);
		Scanner sc=new Scanner(System.in);
		int n,k;
		n=sc.nextInt();
		k=sc.nextInt();
		int[] arr=new int[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=sc.nextInt();
		}
		System.out.println(solve(arr, n, k, 0));
		
		
		
		sc.close();

	}

}
