import java.util.Arrays;
import java.util.Collections;
import java.util.*;
class Main
{
	public static void main (String[] args)
	{
		Scanner sc=new Scanner(System.in); 
		int t,i;	
		int n=sc.nextInt();
		int k=sc.nextInt();
		int h[]=new int[n];
		for(i=0;i<n;i++)
			h[i]=sc.nextInt();
		int dp[]=new int[n];
		Arrays.fill(dp, 0);
		dp[0]=0;
		int ans=0;
		for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		}
		System.out.println(dp[n-1]);
	}
} import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util import java.util.*; util.* java class Main
{
	public static void main (String[] args)
	{
		Scanner sc=new Scanner(System.in); 
		int t,i;	
		int n=sc.nextInt();
		int k=sc.nextInt();
		int h[]=new int[n];
		for(i=0;i<n;i++)
			h[i]=sc.nextInt();
		int dp[]=new int[n];
		Arrays.fill(dp, 0);
		dp[0]=0;
		int ans=0;
		for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		}
		System.out.println(dp[n-1]);
	}
} Main public static void main (String[] args)
	{
		Scanner sc=new Scanner(System.in); 
		int t,i;	
		int n=sc.nextInt();
		int k=sc.nextInt();
		int h[]=new int[n];
		for(i=0;i<n;i++)
			h[i]=sc.nextInt();
		int dp[]=new int[n];
		Arrays.fill(dp, 0);
		dp[0]=0;
		int ans=0;
		for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		}
		System.out.println(dp[n-1]);
	} main {
		Scanner sc=new Scanner(System.in); 
		int t,i;	
		int n=sc.nextInt();
		int k=sc.nextInt();
		int h[]=new int[n];
		for(i=0;i<n;i++)
			h[i]=sc.nextInt();
		int dp[]=new int[n];
		Arrays.fill(dp, 0);
		dp[0]=0;
		int ans=0;
		for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		}
		System.out.println(dp[n-1]);
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int t t i; i int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int h[]=new int[n]; h new int[n] n for(i=0;i<n;i++)
			h[i]=sc.nextInt(); i=0; i=0 i 0 i<n i n i++ i++ i h[i]=sc.nextInt(); h[i]=sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int dp[]=new int[n]; dp new int[n] n Arrays.fill(dp, 0) Arrays.fill Arrays dp 0 dp[0]=0 dp[0] dp 0 0 int ans=0; ans 0 for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		} i=1; i=1 i 1 i<n i n i++ i++ i {
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		} {
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		} int min=Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			} int j=i-k; int j=i-k; j i-k i k j<i j i j++ j++ j {
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			} {
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			} if(j<0)
					continue; j<0 j 0 continue; min=Math.min(min, dp[j]+Math.abs(h[j]-h[i])) min Math.min(min, dp[j]+Math.abs(h[j]-h[i])) Math.min Math min dp[j]+Math.abs(h[j]-h[i]) dp[j] dp j Math.abs(h[j]-h[i]) Math.abs Math h[j]-h[i] h[j] h j h[i] h i dp[i]=min dp[i] dp i min 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
import java.util.Arrays;
import java.util.Collections;
import java.util.*;
class Main
{
	public static void main (String[] args)
	{
		Scanner sc=new Scanner(System.in); 
		int t,i;	
		int n=sc.nextInt();
		int k=sc.nextInt();
		int h[]=new int[n];
		for(i=0;i<n;i++)
			h[i]=sc.nextInt();
		int dp[]=new int[n];
		Arrays.fill(dp, 0);
		dp[0]=0;
		int ans=0;
		for(i=1;i<n;i++)
		{
			int min=Integer.MAX_VALUE;
			for(int j=i-k;j<i;j++)
			{
				if(j<0)
					continue;
				min=Math.min(min, dp[j]+Math.abs(h[j]-h[i]));
			}
			dp[i]=min;
		}
		System.out.println(dp[n-1]);
	}
}