from collections import defaultdict S = input() memo = [0]*(len(S)) n = 0 for i,s in enumerate(S): if s == "A": n += 1 else: n -= 1 memo[i] = n first = defaultdict(int) last = defaultdict(int) for i,m in enumerate(memo): if first[m]: last[m] = i+1 else: first[m] = i+1 ans = 0 for k,v in first.items(): if last[k]: ans = max(ans, last[m]-first[m]) print(ans)