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