main = do
  getLine
  s <- lines <$> getContents
  let xs = map searchAB s
  let xsum = sum $ map (\(i, _, _) -> i) xs
  let endAsum = length $ filter (\(_, l, _) -> l) xs
  let startBsum = length $ filter (\(_, _, l) -> l) xs
  let loneAorB = any (\(_, l, m) -> l == m) xs
  print $ xsum + min 0 (min endAsum startBsum - (if loneAorB then 0 else 1))
  
searchAB s = (countAB, isendA, isstartB)
  where
    countAB = length . filter (== True) $ zipWith isAB s (tail s)
    isAB x y = if x == 'A' && y == 'B' then True else False
    isendA = last s == 'A'
    isstartB = head s == 'B'
