s=input() cnt=[0]*(len(s)+1) now=0 for i in range(len(s)): if s[i]=='A': now+=1 else: now-=1 cnt[i+1]=now if min(cnt)<0: d=min(cnt) for i in range(len(s)+1): cnt[i]+=d ind=[-1]*(2*10**5) ans=0 for i in range(len(s)+1): if ind[cnt[i]]==-1: ind[cnt[i]]=i else: ans=max(ans,i-ind[cnt[i]]) print(ans)