{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B
import Data.Attoparsec.ByteString.Char8
import Data.Sequence (ViewL(..), (|>), (><), empty, fromList, viewl)
import Data.Foldable

type Input = [Bool]

input :: Parser [Bool]
input = map f <$> many1 digit <* endOfLine
  where
    f '1' = True
    f '0' = False
    f _ = error "parse err"

main :: IO ()
main = B.interact
  $ B.pack . show . either (error "parse err") solve . parseOnly input

solve :: Input -> Int
solve [] = 0
solve [_] = 0
solve (x:y:zs) = go empty x y (fromList zs)
  where
    go as x y bs
      | x /= y = (+2) $ solve $ toList $ as >< bs
      | otherwise = case viewl bs of
        (b :< bs) -> go (as |> x) y b bs
        EmptyL
          | all (== True) as -> 0
          | all (== False) as -> 0
          | otherwise -> solve $ toList $ as