S = input() X = [1 << 19] * (1 << 19) X[0] = -1 t = 0 ma = 0 for i, a in enumerate(S): if a == "A": t += 1 else: t -= 1 ma = max(ma, i - X[t]) X[t] = min(X[t], i) print(ma)