from collections import defaultdict S=input() A=[0] for s in S: if s=="A": A.append(A[-1]+1) else: A.append(A[-1]-1) D=defaultdict(list) for i in range(len(A)): D[A[i]].append(i) ANS=0 for d in D: ANS=max(ANS,D[d][-1]-D[d][0]) print(ANS)