S = input() A = [0 for _ in range(len(S) + 1)] B = [0 for _ in range(len(S) + 1)] for i in range(len(S)): if S[i] == 'A': A[i + 1] = 1 else: B[i + 1] = 1 for i in range(len(S)): A[i + 1] += A[i] B[i + 1] += B[i] D = [A[i] - B[i] for i in range(len(S) + 1)] P = [[] for _ in range(len(S) + 1)] for i in range(len(S) + 1): P[D[i]].append(i) for i in range(len(S) + 1): P[i].sort() res = 0 for i in range(len(S) + 1): if P[i]: res = max(res, P[i][-1] - P[i][0]) print(res)