S = input() n = len(S) M = [-1] * (2 * n + 1) m = [n] * (2 * n + 1) x = 0 m[n] = -1 for i in range(n): if S[i] == 'A': x += 1 else: x -= 1 M[x + n] = i if m[x + n] == n: m[x + n] = i print(max(M[i] - m[i] for i in range(2 * n + 1)))