#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string S; cin >> S; const int N = S.size(); int cur = 0, res = 0; map mp; mp[0] = 0; for (int i = 0; i < N; i++) { cur += S[i] == 'A' ? 1 : -1; if (mp.find(cur) == mp.end()) { mp[cur] = i + 1; } else { res = max(res, i + 1 - mp[cur]); } } cout << res << '\n'; return 0; }