import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad.State (State, evalState, runState, put, gets, modify)
import Data.Map.Strict (Map, lookup, insert, empty)

main :: IO ()
main = getLine >> readLine >>= print . solve

solve :: [Int] -> Int
solve xs = evalMemo calc (0, xs)

type Key = (Int, [Int])

calc :: Memoizer Key Int -> Memoizer Key Int
calc f (n, w : x : y : zs) = min <$> res1 <*> res2
    where
        res1 = ((abs $ w - x) +) <$> f (n + 1, x : y : zs)
        res2 = ((abs $ w - y) +) <$> f (n + 2, y : zs)
calc _ (_, [x, y]) = return . abs $ x - y
calc _ _ = return 0

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

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

runMemo :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> (b, Map a b)
runMemo f = flip runState empty . memoizeFix f

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

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine