from collections import * S = input() N = len(S) A = [None] * N for i in range(N): if S[i] == "A": A[i] = 1 else: A[i] = -1 Ac = [0] * (N + 1) for i in range(N): Ac[i + 1] = Ac[i] + A[i] D = dict() for i in range(N + 1): D[Ac[i]] = i ans = 0 for i in range(N + 1): ans = max(ans, D[Ac[i]] - i) print(ans)