# import sys; input = sys.stdin.buffer.readline # sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) import random #処理内容 def main(): L = input() N = len(L) D = defaultdict(lambda:-1) D[0] = 0 ans = 0 val = 0 for i in range(N): if L[i] == "A": val += 1 else: val -= 1 if D[val] != -1: ans = max(ans, i - D[val]) D[val] = i print(ans) if __name__ == '__main__': main()