
import java.util.*;
import java.io.*;
 
 
public class Main  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static ArrayList<Integer>[] graph2; 
    static int n=0;
    static int m=0;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static ArrayList<Integer> list = new ArrayList<>();
    static boolean cycle= false;
    static boolean[] vis;
    static boolean[] vis2;
    static int max=0;
    static int maxnode=0;
    static int[] arr;
    static long[] ans;
    static long[] sum;
    static int[] size;

    static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    }

    /*static void dfs1(int a,int p){
        size[a]=1;
        for(int v: graph[a]){
            if(v!=p){
                dfs1(v,a);
                size[a]+=size[v];
                sum[a]+= (size[v]+sum[v]);
            }
        }
    }
    static void dfs2(int a,int p){
        if(p==-1)
            ans[a]= sum[a];
        else{
            ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a];
        }
        for(int v:graph[a]){
            if(v!=p){
                dfs2(v,a);
            }
        }
    }*/

    
    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
}
 
class trienode{
        int v;
        trienode[] child ;
        boolean leaf;
        int cnt;
        int p=0;
        trienode(int vv){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        }
        trienode(){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        }
    }
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
 
    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
 
    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
 
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}
class newcomparator implements Comparator<Integer>{
    //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    }
 }
class node {
    int f;
    int s;// cost to rreach\
    node(int ff,int ss){
        f=ff;
        s=ss;
    }
}
class mergesort{
    static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    }
    static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    }
    
}
 
  import java.util.*; util.* java import java.io.*; io.* java public class Main  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static ArrayList<Integer>[] graph2; 
    static int n=0;
    static int m=0;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static ArrayList<Integer> list = new ArrayList<>();
    static boolean cycle= false;
    static boolean[] vis;
    static boolean[] vis2;
    static int max=0;
    static int maxnode=0;
    static int[] arr;
    static long[] ans;
    static long[] sum;
    static int[] size;

    static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    }

    /*static void dfs1(int a,int p){
        size[a]=1;
        for(int v: graph[a]){
            if(v!=p){
                dfs1(v,a);
                size[a]+=size[v];
                sum[a]+= (size[v]+sum[v]);
            }
        }
    }
    static void dfs2(int a,int p){
        if(p==-1)
            ans[a]= sum[a];
        else{
            ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a];
        }
        for(int v:graph[a]){
            if(v!=p){
                dfs2(v,a);
            }
        }
    }*/

    
    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
} Main static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) static ArrayList<Integer>[] graph; graph static ArrayList<Integer>[] graph2; graph2 static int n=0; n 0 static int m=0; m 0 static int k =0; k 0 static long mod =1000000007; mod 1000000007 static int MOD = 1000000007; MOD 1000000007 static int[] dx = {0,0,1,-1}; dx {0,0,1,-1} 0 0 1 -1 1 static int[] dy = {1,-1,0,0}; dy {1,-1,0,0} 1 -1 1 0 0 static ArrayList<Integer> list = new ArrayList<>(); list new ArrayList<>() static boolean cycle= false; cycle false static boolean[] vis; vis static boolean[] vis2; vis2 static int max=0; max 0 static int maxnode=0; maxnode 0 static int[] arr; arr static long[] ans; ans static long[] sum; sum static int[] size; size public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    } main {
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    } Reader.init(System.in) Reader.init Reader System.in System System.in int t = 1; t 1 while(t-->0){
           solve();
        } t-->0 t-- t 0 {
           solve();
        } solve() solve out.close() out.close out String[] args args static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    } solve {
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    } n = Reader.nextInt() n Reader.nextInt() Reader.nextInt Reader k = Reader.nextInt() k Reader.nextInt() Reader.nextInt Reader arr = new int[n] arr new int[n] n for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i] = Reader.nextInt(); arr[i] = Reader.nextInt() arr[i] arr i Reader.nextInt() Reader.nextInt Reader int[] dp= new int[n+1]; dp new int[n+1] n+1 n 1 dp[0]=0 dp[0] dp 0 0 dp[1] = Math.abs(arr[1]-arr[0]) dp[1] dp 1 Math.abs(arr[1]-arr[0]) Math.abs Math arr[1]-arr[0] arr[1] arr 1 arr[0] arr 0 for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[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 && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        } {
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        } dp[i] = Integer.MAX_VALUE dp[i] dp i Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[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(arr[i]-arr[i-j]));
            } {
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            } dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j])) dp[i] dp i Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j])) Math.min Math dp[i] dp i dp[i-j] + Math.abs(arr[i]-arr[i-j]) dp[i-j] dp i-j i j Math.abs(arr[i]-arr[i-j]) Math.abs Math arr[i]-arr[i-j] arr[i] arr i arr[i-j] arr i-j i j out.println(dp[n-1]) out.println out dp[n-1] dp n-1 n 1 // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    } gcd {
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    } if(b==0)
            return a;
        else
            return gcd(b,a%b); b==0 b 0 return a; a return gcd(b,a%b); gcd(b,a%b) gcd b a%b a b int a a int b b // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    } lcm {
        return (a*b)/gcd(a,b);
    } return (a*b)/gcd(a,b); (a*b)/gcd(a,b) (a*b) a b gcd(a,b) gcd a b int a a int b b // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    } fastpow {
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    } long res = 1; res 1 a=a%mod a a%mod a mod while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        } b>0 b 0 {
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        } if(b%2==1)
                res = (res*a)%mod; b%2==1 b%2 b 2 1 res = (res*a)%mod res (res*a)%mod (res*a) res a mod a = (a*a)%mod a (a*a)%mod (a*a) a a mod b = b>>1 b b>>1 b 1 return res; res long a a long b b class trienode{
        int v;
        trienode[] child ;
        boolean leaf;
        int cnt;
        int p=0;
        trienode(int vv){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        }
        trienode(){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        }
    } trienode int v; v trienode[] child ; child boolean leaf; leaf int cnt; cnt int p=0; p 0 trienode(int vv){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        } trienode {
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        } child = new trienode[2] child new trienode[2] 2 leaf=false leaf false cnt=0 cnt 0 v=vv v vv int vv vv trienode(){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        } trienode {
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        } child = new trienode[2] child new trienode[2] 2 leaf=false leaf false cnt=0 cnt 0 v=0 v 0 class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
 
    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
 
    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
 
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
} Reader static BufferedReader reader; reader static StringTokenizer tokenizer; tokenizer /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } init {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } reader = new BufferedReader(
                     new InputStreamReader(input) ) reader new BufferedReader(
                     new InputStreamReader(input) ) tokenizer = new StringTokenizer("") tokenizer new StringTokenizer("") InputStream input input /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    } next {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    } while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        } ! tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        } tokenizer = new StringTokenizer(
                   reader.readLine() ) tokenizer new StringTokenizer(
                   reader.readLine() ) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    } nextInt {
        return Integer.parseInt( next() );
    } return Integer.parseInt( next() ); Integer.parseInt( next() ) Integer.parseInt Integer next() next static long nextLong() throws IOException {
        return Long.parseLong( next() );
    } nextLong {
        return Long.parseLong( next() );
    } return Long.parseLong( next() ); Long.parseLong( next() ) Long.parseLong Long next() next static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    } nextDouble {
        return Double.parseDouble( next() );
    } return Double.parseDouble( next() ); Double.parseDouble( next() ) Double.parseDouble Double next() next class newcomparator implements Comparator<Integer>{
    //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    }
 } newcomparator //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    } compare {
        return a<=b?1:-1;
    } return a<=b?1:-1; a<=b?1:-1 a<=b a b 1 -1 1 Integer a a Integer  b b Comparator class node {
    int f;
    int s;// cost to rreach\
    node(int ff,int ss){
        f=ff;
        s=ss;
    }
} node int f; f int s;// cost to rreach\ s node(int ff,int ss){
        f=ff;
        s=ss;
    } node {
        f=ff;
        s=ss;
    } f=ff f ff s=ss s ss int ff ff int ss ss class mergesort{
    static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    }
    static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    }
    
} mergesort static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    } sort {
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    } if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        } l<h l h {
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        } int mid = (l+h)/2; mid (l+h)/2 (l+h) l h 2 sort(l,mid,arr) sort l mid arr sort(mid+1,h,arr) sort mid+1 mid 1 h arr merge(l,mid,h,arr) merge l mid h arr int l l int h h int[] arr arr static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    } merge {

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    } int[] left = new int[m-l+1]; left new int[m-l+1] m-l+1 m-l m l 1 int[] right = new int[h-m]; right new int[h-m] h-m h m for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        } int i= 0 ; int i= 0 ; i 0 i< m-l+1 i m-l+1 m-l m l 1 i++ i++ i {
            left[i] = arr[l+i];
        } {
            left[i] = arr[l+i];
        } left[i] = arr[l+i] left[i] left i arr[l+i] arr l+i l i for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        } int i=0; int i=0; i 0 i<h-m i h-m h m i++ i++ i {
            right[i] = arr[m+1+i];
        } {
            right[i] = arr[m+1+i];
        } right[i] = arr[m+1+i] right[i] right i arr[m+1+i] arr m+1+i m+1+i m 1 i int i=l; i l int lindex = 0; lindex 0 int rindex =  0; rindex 0 while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        } lindex<m-l+1 && rindex<h-m lindex<m-l+1 lindex m-l+1 m-l m l 1 rindex<h-m rindex h-m h m {
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        } if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            } left[lindex]<=right[rindex] left[lindex] left lindex right[rindex] right rindex {
                arr[i]=left[lindex];
                lindex++;
                i++;
            } arr[i]=left[lindex] arr[i] arr i left[lindex] left lindex lindex++ lindex i++ i {
                arr[i]=right[rindex];
                rindex++;
                i++;
            } arr[i]=right[rindex] arr[i] arr i right[rindex] right rindex rindex++ rindex i++ i while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        } lindex<m-l+1 lindex m-l+1 m-l m l 1 {
            arr[i]=left[lindex];
            lindex++;
            i++;
        } arr[i]=left[lindex] arr[i] arr i left[lindex] left lindex lindex++ lindex i++ i while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        } rindex<h-m rindex h-m h m {
            arr[i]=right[rindex];
            rindex++;
            i++;
        } arr[i]=right[rindex] arr[i] arr i right[rindex] right rindex rindex++ rindex i++ i int l l int m m int h h int [] arr arr public class Main  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static ArrayList<Integer>[] graph2; 
    static int n=0;
    static int m=0;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static ArrayList<Integer> list = new ArrayList<>();
    static boolean cycle= false;
    static boolean[] vis;
    static boolean[] vis2;
    static int max=0;
    static int maxnode=0;
    static int[] arr;
    static long[] ans;
    static long[] sum;
    static int[] size;

    static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    }

    /*static void dfs1(int a,int p){
        size[a]=1;
        for(int v: graph[a]){
            if(v!=p){
                dfs1(v,a);
                size[a]+=size[v];
                sum[a]+= (size[v]+sum[v]);
            }
        }
    }
    static void dfs2(int a,int p){
        if(p==-1)
            ans[a]= sum[a];
        else{
            ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a];
        }
        for(int v:graph[a]){
            if(v!=p){
                dfs2(v,a);
            }
        }
    }*/

    
    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
} public class Main  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static ArrayList<Integer>[] graph2; 
    static int n=0;
    static int m=0;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static ArrayList<Integer> list = new ArrayList<>();
    static boolean cycle= false;
    static boolean[] vis;
    static boolean[] vis2;
    static int max=0;
    static int maxnode=0;
    static int[] arr;
    static long[] ans;
    static long[] sum;
    static int[] size;

    static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    }

    /*static void dfs1(int a,int p){
        size[a]=1;
        for(int v: graph[a]){
            if(v!=p){
                dfs1(v,a);
                size[a]+=size[v];
                sum[a]+= (size[v]+sum[v]);
            }
        }
    }
    static void dfs2(int a,int p){
        if(p==-1)
            ans[a]= sum[a];
        else{
            ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a];
        }
        for(int v:graph[a]){
            if(v!=p){
                dfs2(v,a);
            }
        }
    }*/

    
    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
} Main

import java.util.*;
import java.io.*;
 
 
public class Main  {
 
    static PrintWriter out = new PrintWriter(System.out);
    
    public static void main(String[] args) throws IOException{
        //Reader.init(System.in);
        
        Reader.init(System.in);
        int t = 1;//Reader.nextInt();
        while(t-->0){
           solve();
        }
        out.close();
    }
    
    static ArrayList<Integer>[] graph; 
    static ArrayList<Integer>[] graph2; 
    static int n=0;
    static int m=0;
    static int k =0;
    static long mod =1000000007;
    static int MOD = 1000000007;
    static int[] dx = {0,0,1,-1};
    static int[] dy = {1,-1,0,0};
    static ArrayList<Integer> list = new ArrayList<>();
    static boolean cycle= false;
    static boolean[] vis;
    static boolean[] vis2;
    static int max=0;
    static int maxnode=0;
    static int[] arr;
    static long[] ans;
    static long[] sum;
    static int[] size;

    static void solve()throws IOException{
        n = Reader.nextInt();
        k = Reader.nextInt(); 
        arr = new int[n];
        for(int i=0;i<n;i++)
            arr[i] = Reader.nextInt();
        int[] dp= new int[n+1];
        dp[0]=0;
        dp[1] = Math.abs(arr[1]-arr[0]);
        for(int i=2;i<n;i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j=1;j<=k && i-j>=0; j++){
                dp[i] = Math.min(dp[i],dp[i-j] + Math.abs(arr[i]-arr[i-j]));
            }
        }
        out.println(dp[n-1]);
        
    }

    /*static void dfs1(int a,int p){
        size[a]=1;
        for(int v: graph[a]){
            if(v!=p){
                dfs1(v,a);
                size[a]+=size[v];
                sum[a]+= (size[v]+sum[v]);
            }
        }
    }
    static void dfs2(int a,int p){
        if(p==-1)
            ans[a]= sum[a];
        else{
            ans[a] = ans[p] - (sum[a]+size[a]) + (n-size[a]) +sum[a];
        }
        for(int v:graph[a]){
            if(v!=p){
                dfs2(v,a);
            }
        }
    }*/

    
    // ggratest common divisor
    static int gcd(int a , int b){
        if(b==0)
            return a;
        else
            return gcd(b,a%b);
    }
 
    // least common multiple
    static int lcm(int a, int b){
        return (a*b)/gcd(a,b);
    }
    
    // a^b
    static long fastpow(long a, long b){
        long res = 1;a=a%mod;
        while(b>0){
            if(b%2==1)
                res = (res*a)%mod;
            a = (a*a)%mod;
            b = b>>1;
        }
        return res;
    }

    // segment tree
   /* static void update(int ind, int l, int h, int pos, int v){
        if(l==h){
            seg[ind] = v;
            return;
        }
        int mid = (l+h)/2;
        if(pos<=mid)
            update(2*ind,l,mid,pos,v);
        else
            update(2*ind+1,mid+1,h,pos,v);
        seg[ind] = seg[2*ind]^seg[2*ind+1];
    }
    static int query(int ind, int l , int h, int ll , int hh){
        if(ll>hh)
            return 0;
        if(l==ll && h==hh)
            return seg[ind];
        int mid = (l+h)/2;
        return query(2*ind,l,mid,ll,Math.min(mid,hh)) ^ query(2*ind+1,mid+1,h,Math.max(mid+1,ll),hh);
    }*/
    // priority queue with comparator
    // PriorityQueue<Interval> pq = new PriorityQueue<>(new Comparator<Interval>() {
    //             public int compare(Interval a, Interval b){
    //                 int diff1 = a.r - a.l;
    //                 int diff2 = b.r - b.l;
 
    //                 if(diff1 == diff2){
    //                     return a.l - b.l;
    //                 }
 
    //                 return diff2 - diff1;
    //             }
    // });
}
 
class trienode{
        int v;
        trienode[] child ;
        boolean leaf;
        int cnt;
        int p=0;
        trienode(int vv){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=vv;
        }
        trienode(){
            child = new trienode[2];
            leaf=false;
            cnt=0;
            v=0;
        }
    }
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
 
    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
 
    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
 
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}
class newcomparator implements Comparator<Integer>{
    //@Override
    public int compare(Integer a, Integer  b){
        return a<=b?1:-1;
    }
 }
class node {
    int f;
    int s;// cost to rreach\
    node(int ff,int ss){
        f=ff;
        s=ss;
    }
}
class mergesort{
    static void sort(int l, int h, int[] arr){
        if(l<h){
            int mid = (l+h)/2;
            
            sort(l,mid,arr);
            sort(mid+1,h,arr);
            merge(l,mid,h,arr);
        }
    }
    static void merge(int l, int m , int h , int [] arr){

        int[] left = new int[m-l+1];
        int[] right = new int[h-m];
        for(int i= 0 ;i< m-l+1;i++){
            left[i] = arr[l+i];
        }
        for(int i=0;i<h-m;i++){
            right[i] = arr[m+1+i];
        }
        //now left and right arrays are assumed to be sorted and we have tp merge them together 
        // int the original aaray.
        int i=l;
        int lindex = 0;
        int rindex =  0;
        
        while(lindex<m-l+1 && rindex<h-m){
            if(left[lindex]<=right[rindex]){
                arr[i]=left[lindex];
                lindex++;
                i++;
            }
            else{
                arr[i]=right[rindex];
                rindex++;
                i++;
            }
        }
        while(lindex<m-l+1){
            arr[i]=left[lindex];
            lindex++;
            i++;
        }
        while(rindex<h-m){
            arr[i]=right[rindex];
            rindex++;
            i++;
        }
    }
    
}
 
 
