import sys input=lambda: sys.stdin.readline().rstrip() import collections D=collections.defaultdict(int) S=input() cur=0 ans=0 for i in range(len(S)): if S[i]=="A": cur+=1 else: cur-=1 if cur==0: ans=i+1 if D[cur]==0: D[cur]=i+1 else: ans=max(ans,i+1-D[cur]) print(ans)