S=input() T=sorted(S) c=0 while list(S)!=T: c+=1 S=S.replace('BA','AB',1) print(c)