import Data.Array((!), listArray)
main = interact $ show.helper.(map read).words

helper (x:k:xs) = fonk x a 1 k
            where a = listArray (1, x) xs

fonk x a i k = f i
        where 
            f i 
                | x == i = 0
                | x - 1 == i  = abs((a!i) - a!(i+1))
                | otherwise = minn x ll a (i+1) i k 
                where ll = listArray (1, x) $ map f [1..x]


minn x ll a i f k
        | i == x || i-f == k = y
        | otherwise = min y $ minn x ll a (i+1) f k
        where y = (ll!i) + abs(a!f - a!i)
