{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString.Char8 as B
import Data.Attoparsec.ByteString.Char8
import Data.Sequence (Seq(..), (<|), (|>), (><))
import qualified Data.Sequence as C
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 C.empty x y (C.fromList zs)
  where
    go as True False bs = (+2) $ solve $ toList $ as >< bs
    go as False True bs = (+2) $ solve $ toList $ as >< bs
    go as x y Empty
      | all (== True) as = 0
      | all (== False) as = 0
      | otherwise = solve $ toList $ as
    go as x y (b :<| bs) = go (as |> x) y b bs