S=input() v={0:-1} m=y=0 for i,c in enumerate(S): y+=(c>'A')*2-1 if y in v: m=max(m,i-v[y]) else: v[y]=i print(m)