S=input() list1=[0] for i in range(len(S)): if S[i]=="A": list1.append(list1[i]+1) else: list1.append(list1[i]-1) #print(list1) ans=0 for j in range(max(list1)-min(list1)): if list1.count(min(list1)+j)>=2: for k in range(len(S)+1): if list1[k]==min(list1)+j: #print(k) for l in range(len(S)+1): if list1[len(S)-l]==min(list1)+j: #print(len(S)-1-l) if ans