#include using namespace std; int main() { string S; cin >> S; int N = S.size() + 1; vector V(N); for (int i = 0, cnt = 0; i < S.size(); i++) { if (S.at(i) == 'A') cnt++, V.at(i + 1) = cnt; else cnt--, V.at(i + 1) = cnt; } vector A(N * 2, -1), B(N * 2, -1); for (int i = 0; i < N; i++) { if (A.at(V.at(i) + N) == -1) A.at(V.at(i) + N) = i; else B.at(V.at(i) + N) = i; } int ans = 0; for (int i = 0; i < N * 2; i++) { if (B.at(i) == -1) continue; ans = max(ans, B.at(i) - A.at(i)); } cout << ans << "\n"; }