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 = last $ converge $ iterate annihilate cubes
  in
    length cubes - length convergence

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

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