s=input() d={0:-1} p=0 a=0 for i in range(len(s)): c=s[i] p+=c=="A" p-=c=="B" if p in d: a=max(a,i-d[p]) else: d[p]=i print(a)