s = input() l = len(s) bef = [999999 for _ in range(2 * l + 1)] aft = [-1 for _ in range(2 * l + 1)] cnt = 0 bef[l] = 0 for i, ss in enumerate(s): if ss == "A": cnt += 1 else: cnt -= 1 bef[l + cnt] = min(bef[l + cnt], i) aft[l + cnt] = max(aft[l + cnt], i) ans = 0 for b, a in zip(bef, aft): ans = max(a - b, ans) print(ans)