#include using namespace std; signed main() { ios::sync_with_stdio(false); string S; cin >> S; vector pa(S.size() + 1), pb(S.size() + 1); map minpos, maxpos; minpos[0] = maxpos[0] = 0; for (int i = 0; i < S.size(); ++i) { pa[i + 1] = pa[i] + (S[i] == 'A'); pb[i + 1] = pb[i] + (S[i] == 'B'); int d = pa[i + 1] - pb[i + 1]; if (minpos.count(d)) minpos[d] = min(minpos[d], i); else minpos[d] = i; maxpos[d] = max(maxpos[d], i); } int ans = 0; for (auto it = minpos.begin(); it != minpos.end(); ++it) { if (maxpos.count(it->first)) { ans = max(ans, maxpos[it->first] - it->second); } } cout << ans << endl; return 0; }