#include using namespace std; int main() { string s; cin >> s; int n = s.size(); map mp; int cnt = 0; int ans = 0; mp[0] = -1; for (int i = 0; i < n; i++) { if (s[i] == 'A') cnt++; else cnt--; if (mp.count(cnt)) { ans = max(ans , i - mp[cnt]); //cout << i << " : " << ans << endl; } else { mp[cnt] = i; } } cout << ans << endl; return 0; }