<>;($S,$_)=<>;++$%while$S=~s/A//*s/A//||$S=~s/B//*s/B//;print$%