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