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