import java.util.*;
import java.io.*;
class Main {
  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];
    for(int i = 0; i<n; i++)
      a[i] = sc.nextInt();
    int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    }
    System.out.println(dp[n-1]);
  }
} import java.util.*; util.* java import java.io.*; io.* java class Main {
  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];
    for(int i = 0; i<n; i++)
      a[i] = sc.nextInt();
    int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    }
    System.out.println(dp[n-1]);
  }
} Main 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];
    for(int i = 0; i<n; i++)
      a[i] = sc.nextInt();
    int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    }
    System.out.println(dp[n-1]);
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int k = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++)
      a[i] = sc.nextInt();
    int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    }
    System.out.println(dp[n-1]);
  } 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 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 int[] dp = new int[n]; dp new int[n] n Arrays.fill(dp, Integer.MAX_VALUE) Arrays.fill Arrays dp Integer.MAX_VALUE Integer Integer.MAX_VALUE dp[0] = 0 dp[0] dp 0 0 for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    } int i = 1; int i = 1; i 1 i<n i n i++ i++ i {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    } {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    } for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        } int jump = 1; int jump = 1; jump 1 jump<=k jump k jump++ jump++ jump {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        } {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        } if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i])); i-jump>=0 i-jump i jump 0 dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i])) dp[i] dp i Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i])) Math.min Math dp[i] dp i dp[i-jump]+Math.abs(a[i-jump]-a[i]) dp[i-jump] dp i-jump i jump Math.abs(a[i-jump]-a[i]) Math.abs Math a[i-jump]-a[i] a[i-jump] a i-jump i jump a[i] a i 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) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int k = sc.nextInt();
    int[] a = new int[n];
    for(int i = 0; i<n; i++)
      a[i] = sc.nextInt();
    int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for(int i = 1; i<n; i++) {
        for(int jump = 1; jump<=k; jump++) {
            if(i-jump>=0)
                dp[i] = Math.min(dp[i], dp[i-jump]+Math.abs(a[i-jump]-a[i]));
        }
    }
    System.out.println(dp[n-1]);
  }
}