
import java.util.*;

public class Main {

    public static Scanner in = new Scanner(System.in);
    public static int n = 0, k = 0;
    public static int[] h = new int[100001];
    public static long[] dp = new long[100001];
    public final static long oo = 10000000000l;

    public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    }

} import java.util.*; util.* java public class Main {

    public static Scanner in = new Scanner(System.in);
    public static int n = 0, k = 0;
    public static int[] h = new int[100001];
    public static long[] dp = new long[100001];
    public final static long oo = 10000000000l;

    public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    }

} Main public static Scanner in = new Scanner(System.in); in new Scanner(System.in) public static int n = 0 n 0 k = 0; k 0 public static int[] h = new int[100001]; h new int[100001] 100001 public static long[] dp = new long[100001]; dp new long[100001] 100001 public final static long oo = 10000000000l; oo 10000000000l public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    } solve {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    } if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        } idx == n - 1 idx n - 1 n 1 {
            return 0;
        } return 0; 0 {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        } if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            } dp[idx] == -1 dp[idx] dp idx -1 1 {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } long min = oo; min oo for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                } int i = 1; int i = 1; i 1 i <= k i k i++ i++ i {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                } {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                } if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    } i + idx < n i + idx i idx n {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    } min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i])) min Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i])) Math.min Math min solve(idx + i) + Math.abs(h[idx] - h[idx + i]) solve(idx + i) solve idx + i idx i Math.abs(h[idx] - h[idx + i]) Math.abs Math h[idx] - h[idx + i] h[idx] h idx h[idx + i] h idx + i idx i return dp[idx] = min; dp[idx] = min dp[idx] dp idx min {
                return dp[idx];
            } return dp[idx]; dp[idx] dp idx int idx idx public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    } main {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    } for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        } int i = 0; int i = 0; i 0 i < 100001 i 100001 i++ i++ i {
            dp[i] = -1;
        } {
            dp[i] = -1;
        } dp[i] = -1 dp[i] dp i -1 1 n = in.nextInt() n in.nextInt() in.nextInt in k = in.nextInt() k in.nextInt() in.nextInt in for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            h[i] = in.nextInt();
        } {
            h[i] = in.nextInt();
        } h[i] = in.nextInt() h[i] h i in.nextInt() in.nextInt in System.out.println(solve(0)) System.out.println System.out System System.out solve(0) solve 0 String[] args args public class Main {

    public static Scanner in = new Scanner(System.in);
    public static int n = 0, k = 0;
    public static int[] h = new int[100001];
    public static long[] dp = new long[100001];
    public final static long oo = 10000000000l;

    public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    }

} public class Main {

    public static Scanner in = new Scanner(System.in);
    public static int n = 0, k = 0;
    public static int[] h = new int[100001];
    public static long[] dp = new long[100001];
    public final static long oo = 10000000000l;

    public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    }

} Main

import java.util.*;

public class Main {

    public static Scanner in = new Scanner(System.in);
    public static int n = 0, k = 0;
    public static int[] h = new int[100001];
    public static long[] dp = new long[100001];
    public final static long oo = 10000000000l;

    public static long solve(int idx) {
        if (idx == n - 1) {
            return 0;
        } else {
            if (dp[idx] == -1) {
                long min = oo;
                for (int i = 1; i <= k; i++) {
                    if (i + idx < n) {
                        min = Math.min(min, solve(idx + i) + Math.abs(h[idx] - h[idx + i]));
                    }
                }
                return dp[idx] = min;
            } else {
                return dp[idx];
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100001; i++) {
            dp[i] = -1;
        }
        n = in.nextInt();
        k = in.nextInt();
        for (int i = 0; i < n; i++) {
            h[i] = in.nextInt();
        }
        System.out.println(solve(0));
    }

}
