{-# LANGUAGE TupleSections #-}
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad.State (State, evalState, gets, modify)
import Data.Map (Map, lookup, insert, empty)
import Data.Maybe (maybe)

main = breads >>= main'
    where
        main' [_, _, a, b] = breads >>= print . solve . (a,) . (b :)

solve :: (Int, [Int])-> Int
solve = run calc
    where
        calc f (a, b : x : xs) = min <$> ((abs (a - x) +) <$> f (x, b : xs)) <*> ((abs (b - x) +) <$> f (a, x : xs))
        calc _ _ = return 0

breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)

type Memoizer a b = a -> State (Map a b) b

run :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> b
run f = flip evalState empty . memoizeFix f

memoizeFix :: (Ord a) => (Memoizer a b -> Memoizer a b) -> Memoizer a b
memoizeFix f x = gets (Data.Map.lookup x) >>= maybe (f (memoizeFix f) x >>= ((<$) <*> (modify . Data.Map.insert x))) return
