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 #print(S) #print(memo) memo = [0]+memo first = defaultdict(lambda:-1) last = defaultdict(lambda:-1) for i,m in enumerate(memo): if first[m] != -1: last[m] = i else: first[m] = i ans = 0 for k,v in first.items(): if last[k] != -1: ans = max(ans, last[m]-first[m]) print(ans)