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