import Control.Monad
import Data.List

main = do
    n <- readLn
    ss <- replicateM n getLine
    let ab = sum $ map  (length . (filter ("AB" `isPrefixOf`)) . tails) ss
        a = length $ filter ((=='A') . last) ss
        b = length $ filter ((=='B') . head) ss
    print $ ab + min a b