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

class Main
{
	static Integer dp[];

	static int minimumJumps(int ar[], int i, int k)
	{
		if(i==0)
			return 0;

		if(dp[i]!=null)
			return dp[i];

		int min=Integer.MAX_VALUE;
		int st=Math.max(i-k, 0);

		for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		}
		return dp[i]=min;
	}

	public static void main(String[] args)
	{
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int k=scan.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++)
			ar[i]=scan.nextInt();

		dp=new Integer[n];

		int ans = minimumJumps(ar, ar.length-1, k);
		System.out.println(ans);
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java class Main
{
	static Integer dp[];

	static int minimumJumps(int ar[], int i, int k)
	{
		if(i==0)
			return 0;

		if(dp[i]!=null)
			return dp[i];

		int min=Integer.MAX_VALUE;
		int st=Math.max(i-k, 0);

		for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		}
		return dp[i]=min;
	}

	public static void main(String[] args)
	{
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int k=scan.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++)
			ar[i]=scan.nextInt();

		dp=new Integer[n];

		int ans = minimumJumps(ar, ar.length-1, k);
		System.out.println(ans);
	}
} Main static Integer dp[]; dp static int minimumJumps(int ar[], int i, int k)
	{
		if(i==0)
			return 0;

		if(dp[i]!=null)
			return dp[i];

		int min=Integer.MAX_VALUE;
		int st=Math.max(i-k, 0);

		for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		}
		return dp[i]=min;
	} minimumJumps {
		if(i==0)
			return 0;

		if(dp[i]!=null)
			return dp[i];

		int min=Integer.MAX_VALUE;
		int st=Math.max(i-k, 0);

		for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		}
		return dp[i]=min;
	} if(i==0)
			return 0; i==0 i 0 return 0; 0 if(dp[i]!=null)
			return dp[i]; dp[i]!=null dp[i] dp i null return dp[i]; dp[i] dp i int min=Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int st=Math.max(i-k, 0); st Math.max(i-k, 0) Math.max Math i-k i k 0 for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		} int j=st; int j=st; j st j<i j i j++ j++ j {
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		} {
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		} if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j])); dp[j]==null dp[j] dp j null min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j])) min Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j])) Math.min Math min minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]) minimumJumps(ar, j, k) minimumJumps ar j k Math.abs(ar[i]-ar[j]) Math.abs Math ar[i]-ar[j] ar[i] ar i ar[j] ar j min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j])) min Math.min(min, dp[j]+Math.abs(ar[i]-ar[j])) Math.min Math min dp[j]+Math.abs(ar[i]-ar[j]) dp[j] dp j Math.abs(ar[i]-ar[j]) Math.abs Math ar[i]-ar[j] ar[i] ar i ar[j] ar j return dp[i]=min; dp[i]=min dp[i] dp i min int ar[] ar int i i int k k public static void main(String[] args)
	{
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int k=scan.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++)
			ar[i]=scan.nextInt();

		dp=new Integer[n];

		int ans = minimumJumps(ar, ar.length-1, k);
		System.out.println(ans);
	} main {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int k=scan.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++)
			ar[i]=scan.nextInt();

		dp=new Integer[n];

		int ans = minimumJumps(ar, ar.length-1, k);
		System.out.println(ans);
	} Scanner scan=new Scanner(System.in); scan new Scanner(System.in) int n=scan.nextInt(); n scan.nextInt() scan.nextInt scan int k=scan.nextInt(); k scan.nextInt() scan.nextInt scan int ar[]=new int[n]; ar new int[n] n for(int i=0;i<n;i++)
			ar[i]=scan.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i ar[i]=scan.nextInt(); ar[i]=scan.nextInt() ar[i] ar i scan.nextInt() scan.nextInt scan dp=new Integer[n] dp new Integer[n] n int ans = minimumJumps(ar, ar.length-1, k); ans minimumJumps(ar, ar.length-1, k) minimumJumps ar ar.length-1 ar.length ar ar.length 1 k System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
	static Integer dp[];

	static int minimumJumps(int ar[], int i, int k)
	{
		if(i==0)
			return 0;

		if(dp[i]!=null)
			return dp[i];

		int min=Integer.MAX_VALUE;
		int st=Math.max(i-k, 0);

		for(int j=st;j<i;j++)
		{
			if(dp[j]==null)
				min = Math.min(min, minimumJumps(ar, j, k)+Math.abs(ar[i]-ar[j]));
			else
				min = Math.min(min, dp[j]+Math.abs(ar[i]-ar[j]));
		}
		return dp[i]=min;
	}

	public static void main(String[] args)
	{
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int k=scan.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++)
			ar[i]=scan.nextInt();

		dp=new Integer[n];

		int ans = minimumJumps(ar, ar.length-1, k);
		System.out.println(ans);
	}
}