S=input() N=len(S) D={0:[-1,-1]} X=0 for i in range(N): if S[i]=="A": X+=1 else: X-=1 if X in D: D[X][1]=i else: D[X]=[i,i] K=0 for x in D: K=max(K,D[x][1]-D[x][0]) print(K)