#include using namespace std; #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif #define int long long signed main(){ string s; cin >> s; int n = s.size(); vector dp(n+1, 0); map mp; for(int i = 0; i < n; i++){ if(s[i] == 'A') dp[i+1] = dp[i] + 1; if(s[i] == 'B') dp[i+1] = dp[i] - 1; mp[dp[i+1]] = i; } int ans = 0; for(int i = 0; i <= n; i++){ ans = max(ans, mp[dp[i]] - i + 1); } if(ans == 1) ans = 0; cout << ans << endl; return 0; }