S = input() N = len(S) cnt = [0] * (N + 1) farthest = {0: 0} ans = 0 for i, s in enumerate(S, start=1): if s == 'A': cnt[i] = cnt[i - 1] + 1 else: cnt[i] = cnt[i - 1] - 1 if cnt[i] not in farthest.keys(): farthest[cnt[i]] = i else: ans = max(ans, i - farthest[cnt[i]]) print(ans)