import java.util.*;

public class Main{
  public static int dp[];
  public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  }
} import java.util.*; util.* java public class Main{
  public static int dp[];
  public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  }
} Main public static int dp[]; dp public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  } func {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  } if(start>=a.length)
    return Integer.MAX_VALUE; start>=a.length start a.length a a.length return Integer.MAX_VALUE; Integer.MAX_VALUE Integer Integer.MAX_VALUE if(start==a.length-1)
    return 0; start==a.length-1 start a.length-1 a.length a a.length 1 return 0; 0 if(dp[start]!=-1)
    return dp[start]; dp[start]!=-1 dp[start] dp start -1 1 return dp[start]; dp[start] dp start int ans=Integer.MAX_VALUE; ans Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    } int i=start+1; int i=start+1; i start+1 start 1 i<=Math.min(start+k,a.length-1) i Math.min(start+k,a.length-1) Math.min Math start+k start k a.length-1 a.length a a.length 1 i++ i++ i {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    } {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    } ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k)) ans Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k)) Math.min Math ans Math.abs(a[start]-a[i])+func(a,i,k) Math.abs(a[start]-a[i]) Math.abs Math a[start]-a[i] a[start] a start a[i] a i func(a,i,k) func a i k return dp[start]=ans; dp[start]=ans dp[start] dp start ans int a[] a int start start int k k public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  } main {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int k=sc.nextInt(); k sc.nextInt() sc.nextInt sc int a[]=new int[n]; a new int[n] n dp=new int[n+1] dp new int[n+1] n+1 n 1 for(int i=0;i<n;i++)
    a[i]=sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=sc.nextInt(); a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc for(int i=0;i<=n;i++)
    dp[i]=-1; int i=0; int i=0; i 0 i<=n i n i++ i++ i dp[i]=-1; dp[i]=-1 dp[i] dp i -1 1 System.out.println(func(a,0,k)) System.out.println System.out System System.out func(a,0,k) func a 0 k String[] args args public class Main{
  public static int dp[];
  public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  }
} public class Main{
  public static int dp[];
  public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  }
} Main
import java.util.*;

public class Main{
  public static int dp[];
  public static int func(int a[],int start,int k)
  {
    if(start>=a.length)
    return Integer.MAX_VALUE;
    if(start==a.length-1)
    return 0;
    if(dp[start]!=-1)
    return dp[start];
    int ans=Integer.MAX_VALUE;
    for(int i=start+1;i<=Math.min(start+k,a.length-1);i++)
    {
      ans=Math.min(ans,Math.abs(a[start]-a[i])+func(a,i,k));
    }
    return dp[start]=ans;
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int k=sc.nextInt();
    int a[]=new int[n];
    dp=new int[n+1];
    for(int i=0;i<n;i++)
    a[i]=sc.nextInt();
    for(int i=0;i<=n;i++)
    dp[i]=-1;
    System.out.println(func(a,0,k));
  }
}