import java.util.*;
import java.io.*;
class Main{

     public static void main(String []args) throws Exception{
        //System.out.println("Hello World");
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int N,k;
        String s;
        s=br.readLine();
        String ss[]=s.split(" ");
     N=Integer.parseInt(ss[0]);
     k=Integer.parseInt(ss[1]);
     
     int arr[]=new int[N];
     s=br.readLine();
     ss=s.split(" ");
     
     for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     }
     if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     }
     }
         
         
         
         
         
     } import java.util.*; util.* java import java.io.*; io.* java class Main{

     public static void main(String []args) throws Exception{
        //System.out.println("Hello World");
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int N,k;
        String s;
        s=br.readLine();
        String ss[]=s.split(" ");
     N=Integer.parseInt(ss[0]);
     k=Integer.parseInt(ss[1]);
     
     int arr[]=new int[N];
     s=br.readLine();
     ss=s.split(" ");
     
     for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     }
     if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     }
     }
         
         
         
         
         
     } Main public static void main(String []args) throws Exception{
        //System.out.println("Hello World");
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int N,k;
        String s;
        s=br.readLine();
        String ss[]=s.split(" ");
     N=Integer.parseInt(ss[0]);
     k=Integer.parseInt(ss[1]);
     
     int arr[]=new int[N];
     s=br.readLine();
     ss=s.split(" ");
     
     for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     }
     if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     }
     } main {
        //System.out.println("Hello World");
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int N,k;
        String s;
        s=br.readLine();
        String ss[]=s.split(" ");
     N=Integer.parseInt(ss[0]);
     k=Integer.parseInt(ss[1]);
     
     int arr[]=new int[N];
     s=br.readLine();
     ss=s.split(" ");
     
     for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     }
     if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     }
     } BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N N k; k String s; s s=br.readLine() s br.readLine() br.readLine br String ss[]=s.split(" "); ss s.split(" ") s.split s " " N=Integer.parseInt(ss[0]) N Integer.parseInt(ss[0]) Integer.parseInt Integer ss[0] ss 0 k=Integer.parseInt(ss[1]) k Integer.parseInt(ss[1]) Integer.parseInt Integer ss[1] ss 1 int arr[]=new int[N]; arr new int[N] N s=br.readLine() s br.readLine() br.readLine br ss=s.split(" ") ss s.split(" ") s.split s " " for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     } int i=0; int i=0; i 0 i<N i N i++ i++ i {
     arr[i]=Integer.parseInt(ss[i]);
     } {
     arr[i]=Integer.parseInt(ss[i]);
     } arr[i]=Integer.parseInt(ss[i]) arr[i] arr i Integer.parseInt(ss[i]) Integer.parseInt Integer ss[i] ss i if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     } N==2 N 2 {
         System.out.println(Math.abs(arr[0]-arr[1]));
     } System.out.println(Math.abs(arr[0]-arr[1])) System.out.println System.out System System.out Math.abs(arr[0]-arr[1]) Math.abs Math arr[0]-arr[1] arr[0] arr 0 arr[1] arr 1 {
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     } long dp[]=new long[N]; dp new long[N] N dp[0]=0l dp[0] dp 0 0l dp[1]=Math.abs(arr[1]-arr[0])*1l dp[1] dp 1 Math.abs(arr[1]-arr[0])*1l Math.abs(arr[1]-arr[0]) Math.abs Math arr[1]-arr[0] arr[1] arr 1 arr[0] arr 0 1l long temp=10000000009l; temp 10000000009l for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     } int i=2; int i=2; i 2 i<N i N i++ i++ i {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     } {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     } temp=1000000009l temp 1000000009l for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         } int j=1; int j=1; j 1 j<=k j k j++ j++ j {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         } {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         } if(i-j<0)
             {
                 continue;
             } i-j<0 i-j i j 0 {
                 continue;
             } continue; long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l; var (dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l (dp[i-j]+Math.abs(arr[i]-arr[i-j])) dp[i-j] dp i-j i j Math.abs(arr[i]-arr[i-j]) Math.abs Math arr[i]-arr[i-j] arr[i] arr i arr[i-j] arr i-j i j 1l temp=Math.min(var,temp) temp Math.min(var,temp) Math.min Math var temp dp[i]=temp dp[i] dp i temp 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.*;
import java.io.*;
class Main{

     public static void main(String []args) throws Exception{
        //System.out.println("Hello World");
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        int N,k;
        String s;
        s=br.readLine();
        String ss[]=s.split(" ");
     N=Integer.parseInt(ss[0]);
     k=Integer.parseInt(ss[1]);
     
     int arr[]=new int[N];
     s=br.readLine();
     ss=s.split(" ");
     
     for(int i=0;i<N;i++)
     {
     arr[i]=Integer.parseInt(ss[i]);
     }
     if(N==2)
     {
         System.out.println(Math.abs(arr[0]-arr[1]));
     }
     else{
     long dp[]=new long[N];
     dp[0]=0l;
     dp[1]=Math.abs(arr[1]-arr[0])*1l;
     long temp=10000000009l;
     for (int i=2;i<N;i++)
     {
         temp=1000000009l;
         for(int j=1;j<=k;j++)
         {
             if(i-j<0)
             {
                 continue;
             }
             long var=(dp[i-j]+Math.abs(arr[i]-arr[i-j]))*1l;
             temp=Math.min(var,temp);
             
         }
         dp[i]=temp;
         
     }
         
         
         
       
System.out.println(dp[N-1]);         
     }
     }
         
         
         
         
         
     }
