function Main(input){
  
  var tmp = input.trim();
  var h = tmp.split("\n");
  var n = h[0];
  var s = h.slice(1);
  var kekka = 0;
  var ab = 0;
  var startB = 0;
  var endA = 0; 
  var banda = 0;
  
  var counter = function(str,seq){
    return str.split(seq).length - 1;
}
  
  for(i=0;i<n;i++){
   var ab = ab + counter(s[i],"AB");
    
    if(s[i].charAt(0)=="B"){
        //console.log("startB kitayo");
      startB++;}
    if(s[i].charAt(s[i].length-1)=="A"){
              //console.log("endA kitayo");
      endA++;}
    //if(s[i].charAt(-1)=="A"&&s[i].charAt(0)=="B"){
      //banda++;}
    
  }//for end i++
  
  if(startB<=endA){
  var kekka = ab + startB;
  }else{
   var kekka = ab + endA;
  }
  
  console.log(kekka);
 
}//function end
 
Main(require("fs").readFileSync("/dev/stdin", "utf8"));