import java.util.*;


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


class Main {
  public static void main(String[] args) {
    int n, k;
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    k = sc.nextInt();
    
    int[] h = new int[n];
    for(int i = 0; i < n; i++) {
      h[i] = sc.nextInt();
    }
    int[] dp = new int[n];
    Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    dp[1] = Math.abs(h[0] - h[1]);
    for(int i = 2; i < n; i++) {
      for(int j = 1; j <= k && i-j >= 0; j++) {
          dp[i] = Math.min(dp[i], dp[i-j] + Math.abs(h[i] - h[i-j]));
      }
    }
   System.out.println(dp[n-1]);
  }
}
