import java.io.*;
import java.util.*;
import java.math.*;
     
public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	}
} import java.io.*; io.* java import java.util.*; util.* java import java.math.*; math.* java public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	}
} Main public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	} main {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	} IO io = new IO(); io new IO() PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) Solver sr = new Solver(); sr new Solver() sr.solve(io,out) sr.solve sr io out out.flush() out.flush out out.close() out.close out String[] args args static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	} Solver void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    } solve {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    } int t = 1; t 1 while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            } t-->0 t-- t 0 {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            } int n = io.nextInt(); n io.nextInt() io.nextInt io int k = io.nextInt(); k io.nextInt() io.nextInt io int[] ar = new int[n]; ar new int[n] n for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt(); int i=0 ; int i=0 ; i 0 i<n i n i++ i++ i ar[i] = io.nextInt(); ar[i] = io.nextInt() ar[i] ar i io.nextInt() io.nextInt io 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 int end=0; end 0 for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                } ; end<n-1 end n-1 n 1 end++ end++ end {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                } {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                } for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  } int i=end+1 ; int i=end+1 ; i end+1 end 1 i<=Math.min(end+k,n-1) i Math.min(end+k,n-1) Math.min Math end+k end k n-1 n 1 i++ i++ i {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  } {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  } dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i])) dp[i] dp i Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i])) Math.min Math dp[i] dp i dp[end]+Math.abs(ar[end]-ar[i]) dp[end] dp end Math.abs(ar[end]-ar[i]) Math.abs Math ar[end]-ar[i] ar[end] ar end ar[i] ar i out.print(dp[n-1]) out.print out dp[n-1] dp n-1 n 1 IO io io PrintWriter out out static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	} IO private static final int BUF_SIZE = 2048; BUF_SIZE 2048 BufferedReader br; br private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        } IO {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        } isSpace {
            return c == '\n' || c == '\r' || c == ' ';
        } return c == '\n' || c == '\r' || c == ' '; c == '\n' || c == '\r' || c == ' ' c == '\n' || c == '\r' || c == ' ' c == '\n' c '\n' c == '\r' c '\r' c == ' ' c ' ' char c c String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } next {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } IOException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } StringBuilder sb = new StringBuilder(); sb new StringBuilder() int r; r while ((r = br.read()) != -1 && isSpace((char) r)); (r = br.read()) != -1 && isSpace((char) r) (r = br.read()) != -1 (r = br.read()) r br.read() br.read br -1 1 isSpace((char) r) isSpace (char) r if (r == -1) {
                    return null;
                } r == -1 r -1 1 {
                    return null;
                } return null; null sb.append((char) r) sb.append sb (char) r while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                } (r = br.read()) != -1 && !isSpace((char) r) (r = br.read()) != -1 (r = br.read()) r br.read() br.read br -1 1 !isSpace((char) r) isSpace((char) r) isSpace (char) r {
                    sb.append((char) r);
                } sb.append((char) r) sb.append sb (char) r return sb.toString(); sb.toString() sb.toString sb return null; null int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	}
} public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	}
} Main
import java.io.*;
import java.util.*;
import java.math.*;
     
public class Main{
	public static void main(String[] args) throws Exception {
	IO io = new IO();
	PrintWriter out = new PrintWriter(System.out);
	Solver sr = new Solver();
	sr.solve(io,out);
	out.flush();
	out.close();
    	}

	static class Solver
	{
        void solve(IO io, PrintWriter out)
        {
            int t = 1;
            while(t-->0)
            {
                int n = io.nextInt();
              	int k = io.nextInt();
              	int[] ar = new int[n];
              	for(int i=0 ; i<n ; i++)
                  ar[i] = io.nextInt();
              	int dp[] = new int[n];
              	Arrays.fill(dp,Integer.MAX_VALUE);
              	dp[0]=0;
              	int end=0;
              	for( ; end<n-1; end++)
                {
                  for(int i=end+1 ; i<=Math.min(end+k,n-1) ; i++)
                  {
                    dp[i] = Math.min(dp[i],dp[end]+Math.abs(ar[end]-ar[i]));
                  }
                  //out.println(dp[end]);
                }
              	out.print(dp[n-1]);
            }
	    }
	}
	//Special thanks to Petr (on codeforces) who inspired me to implement IO class!
	static class IO
	{
    	private static final int BUF_SIZE = 2048;
        BufferedReader br;
        private IO() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        private boolean isSpace(char c) {
            return c == '\n' || c == '\r' || c == ' ';
        }
        String next() {
            try {
                StringBuilder sb = new StringBuilder();
                int r;
                while ((r = br.read()) != -1 && isSpace((char) r));
                if (r == -1) {
                    return null;
                }
                sb.append((char) r);
                while ((r = br.read()) != -1 && !isSpace((char) r)) {
                    sb.append((char) r);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
	}
}