#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), B(N * 2); for (int i = 0; i < N; i++) { if (A.at(V.at(i) + N) == 0) 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++) { ans = max(ans, B.at(i) - A.at(i)); } cout << ans << "\n"; }