import           Data.List  (find)
import           Data.Maybe (fromJust)

main :: IO ()
main = print . solve . map fromChar =<< getLine

data Cube =
    Red
  | Blue
  deriving (Eq)

fromChar :: Char -> Cube
fromChar '0' = Red
fromChar '1' = Blue
fromChar _   = error "unexpected character"

solve :: [Cube] -> Int
solve cubes =
  let
    convergence' = fromJust $ convergence $ iterate annihilate cubes
  in
    length cubes - length convergence'

annihilate :: [Cube] -> [Cube]
annihilate []                = []
annihilate [c]               = [c]
annihilate (Red : Blue : cs) = annihilate cs
annihilate (Blue : Red : cs) = annihilate cs
annihilate (c : cs)          = c : annihilate cs

annihilate' :: [Cube] -> [Cube]
annihilate' cs = foldr f [] $ zip cs $ tail cs
  where
    f (Red, Blue) cs' = cs'
    f (Blue, Red) cs' = cs'
    f (c1, c2) cs'    = c2 : cs'

converge :: Eq a => [a] -> [a]
converge [] = []
converge [x] = [x]
converge (x : xs@(y: _))
  | x == y    = [x]
  | otherwise = x : converge xs

convergence :: Eq a => [a] -> Maybe a
convergence = fmap fst . find (uncurry (==)) . (zip <*> tail)
