#yuki672 from collections import defaultdict s=raw_input() d=defaultdict(int) p=0 d[0]=-1 res=0 for i in xrange(len(s)): if s[i]=='A': p+=1 else: p-=1 if p in d: res=max(res,i-d[p]) else: d[p]=i print res