import Data.List(minimum)

opA :: (Int -> Int)
opA i = 2 * i

opB :: (Int -> Int -> Int)
opB i k = 2 * (abs $ subtract i k)  

likeFor :: (Int -> Int) -> (Int -> Int) -> [Int] -> [Int] -> [Int] 
likeFor opa opb [] acc = acc
likeFor opa opb src acc = likeFor opa opb (tail src) acc'
  where
    h = head src
    acc' = (minimum [opa h, opb h] + (head acc)) : acc

main = do
  ins <- fmap (fmap (read :: String -> Int) . words) . lines <$> getContents
  let ns = last ins
  let k = head $ head $ tail ins
  let result = likeFor opA (opB k) ns [0]
  putStrLn $ show $ head result