from itertools import accumulate def main(): S = [0] + list(input()) N = len(S)-1 a = list(accumulate(S, lambda s, c: s + 1 if c == 'A' else s)) b = list(accumulate(S, lambda s, c: s + 1 if c == 'B' else s)) ans = 0 for l in range(N-1): for r_plus1 in range(l+2, N+1): if a[r_plus1] - a[l] == b[r_plus1] - b[l]: d = r_plus1 - l if d > ans: ans = d print(ans) main()