import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B

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

solve :: String -> Int
solve = (* 2) . count 0 . map length . group
    where
        count acc (w : x : y : zs) 
            | w > x = count (acc + x) $ (w - x + y) : zs
            | w < x = count (acc + w) $ (x - w) : y : zs
            | otherwise = count (acc + w) $ y : zs
        count acc [x, y] = acc + min x y
        count acc _ = acc

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