#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; for (int i = 0; i < N; i++) { cur += S[i] == 'A' ? 1 : -1; if (mp.find(cur) == mp.end()) { mp[cur] = i; } else { res = max(res, i - mp[cur]); } } cout << res << '\n'; return 0; }