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 d = defaultdict(lambda:-1) for i,m in enumerate(memo): if d[m] == -1: d[m] = [i,0] else: d[m][1] = i ans = 0 for v in d.values(): ans = max(ans, v[1]-v[0]) print(ans)