import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.Bool (bool)

main :: IO ()
main = do
  n <- unsafeSignedDecimal <$> T.getLine :: IO Int
  ss <- sequence . replicate n $ T.getLine :: IO [T.Text]
  let
    (ab, a, ba, b) = foldr step (0, 0, 0, 0) $ ss
    ba' = pred ba
    a' = pred a
    b' = pred b
  print $ ab + bool (min a b) (bool 0 1 (a > 0) + ba' + bool 0 1 (b > 0) + min (max a' 0) (max b' 0)) (ba' >= 0)

step :: T.Text -> (Int, Int, Int, Int) -> (Int, Int, Int, Int)
step = g . f
  where
    f = h . T.foldr step' (Nothing, 0, False)
      where
        step' 'A' (Nothing, _, _) = (Just 'A', 0, True)
        step' 'A' (Just 'B', ab, a) = (Just 'A', succ ab, a)
        step' c (_, ab, a) = (Just c, ab, a)
        h (Just 'B', ab, a) = (ab, a, True)
        h (_, ab, a) = (ab, a, False)
    g (ab', True, True) (ab, a, ba, b) = (ab + ab', a, succ ba, b)
    g (ab', a', b') (ab, a, ba, b) = (ab + ab', a + bool 0 1 a', ba, b + bool 0 1 b')

unsafeSignedDecimal :: T.Text -> Int
unsafeSignedDecimal s = case T.signed T.decimal s of
  Right (n, _) -> n
