def main1(s): s=[-1 if x=="A" else 1 for x in s] n=len(s) ss=[0] for x in s:ss.append(ss[-1]+x) d={} for i in range(n+1): if ss[i] in d: d[ss[i]].append(i) else: d[ss[i]]=[i] ans=0 for v in d.values(): if len(v)>=2: ans=max(ans,v[-1]-v[0]) return ans if __name__=='__main__': s=input() print(main1(s))