from collections import defaultdict S = input() cnt = [0] mark_dict = defaultdict(int) ans = 0 for i in range(len(S)): if S[i] == 'A': cnt_i = cnt[-1] + 1 else: cnt_i = cnt[-1] - 1 cnt.append(cnt_i) if cnt_i == 0: ans = max(ans, i+1) else: if mark_dict[cnt_i] == 0: mark_dict[cnt_i] = i else: ans = max(ans, i - mark_dict[cnt_i]) print(ans)