import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    } main {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

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

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();
        int k = scan.nextInt();
        int[] heights = new int[n];
        for (int i = 0; i < n; ++i) {
            heights[i] = scan.nextInt();
        }

        int[] dp = new int[n];
        dp[0] = 0;
        dp[1] = Math.abs(heights[1] - heights[0]);

        for (int i = 2; i < n; ++i) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = 1; j <= k && j <= i; ++j) {
                dp[i] = Math.min(dp[i], dp[i - j] + Math.abs(heights[i] - heights[i - j]));
            }
        }

        System.out.println(dp[n - 1]);
    }
}