{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE Unsafe #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IMS
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST
-- import Control.Monad.ST.Safe
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import System.IO
import Data.Function
import GHC.Magic (oneShot)

main ::  IO ()
main = do
  Just (n,_) <- BS.readInt <$> BS.getLine
  as <- VU.unfoldrN n (BS.readInt . BS.dropWhile (<'-')) <$> BS.getLine
  print $ query as

query :: VU.Vector Int -> Int
query as = VU.minimum totalWork
  where
    totalWork = VU.zipWith (+) incWork decWork
    incWork :: VU.Vector Int
    incWork = VU.reverse $ (VU.replicate 2 0 VU.++) $ (`evalState` (0, Nil))
      $ (`VU.imapM` (VU.reverse incDiff)) $ \i x -> state $ \(!acc, !ls) ->
      if x < 0 then
        (acc, (acc, Cons (i+1) (-x) ls))
      else
        let (!acc1,!newls) = consume x (i+1) ls
            !newAcc = acc + acc1
        in (newAcc, (newAcc, newls))
    decWork = (VU.fromListN 2 [0,1] VU.++) $ (`evalState` (1, Nil))
      $ (`VU.imapM` decDiff) $ \i x -> state $ \(!acc, !ls) ->
      if x < 0 then
        let !newAcc = acc + 1
        in (newAcc, (newAcc, Cons (i+1) (-x) ls))
      else
        let (!acc1, !newls) = consume x (i+1) ls
            !newAcc = acc + acc1 + 1
        in (newAcc, (newAcc, newls))
    incDiff = VU.map (*2) $ VU.zipWith surpassExp4 as (VU.tail as)
    decDiff = VU.map (*2) $ VU.zipWith surpassExp4 (VU.tail as) as

data Stack = Nil | Cons {-# UNPACK #-} !Int {-# UNPACK #-} !Int !Stack

consume :: Int -> Int -> Stack -> (Int, Stack)
consume = loop 0
  where
    loop !acc !consumed !i0 (Cons i1 power rest)
      | consumed < power
        = (acc + consumed * (i0-i1), Cons i1 (power-consumed) rest)
      | otherwise = loop (acc + consumed * (i0-i1)) (consumed-power) i1 rest
    loop !acc !consumed !i0 Nil
      = (acc + consumed * i0, Nil)

-- minimum e with x <= 4^e * y. Assumes x,y > 0.
surpassExp4 x y = (surpassExponent x y + 1) `shiftR` 1

-- minimum e with x <= 2^e * y. Assumes 0 < x,y < 2^62.
surpassExponent :: Int -> Int -> Int
surpassExponent x y
  | xShifted <= yShifted = bitDiff
  | otherwise            = bitDiff + 1
  where
    !xlz = countLeadingZeros x
    !ylz = countLeadingZeros y
    !bitDiff = ylz - xlz
    !xShifted = x `shiftL` (xlz - 2)
    !yShifted = y `shiftL` (ylz - 2)
