main :: IO ()
main = do
  s <- map snd . runLength <$> getLine
  print $ unify s

runLength :: Eq a => [a] -> [(a, Int)]
runLength = foldr step []
  where
    step x ((y, n) : ts) | x == y = (y, n + 1) : ts
    step x ts = (x, 1) : ts

unify :: [Int] -> Int
unify = (2 *) . fst . unify' . (,) 0
  where
    unify' (k, x : y : ls)
      =
        if x > y
          then
            unify'' (x - y) (k + y, ls)
          else
            unify' (k + x, (y - x) : ls)
    unify' t = t
    unify'' x0 (k, x1 : ls) = unify' (k, (x0 + x1) : ls)
    unify'' x0 (k, _) = (k, [x0])
