from collections import defaultdict S = input() mark_dict = defaultdict(list) cnt = [0] mark_dict[0].append(0) 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 len(mark_dict[cnt_i]) == 0: mark_dict[cnt_i].append(i+1) else: ans = max(ans, i - mark_dict[cnt_i][0] + 1) print(ans)