S=input() N=len(S) X=[0]*(N+1) for i in range(N): if S[i]=="A": X[i+1]=X[i]+1 else: X[i+1]=X[i]-1 D={} for i in range(N+1): if X[i] in D: D[X[i]].append(i) else: D[X[i]]=[i] M=0 for a in D: M=max(M,max(D[a])-min(D[a])) print(M)