#include #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair ; using pll = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; int main(){ string s; cin >> s; int n = s.size(); map mp; vector cnt(n+1,0); rep(i,n) cnt[i+1] = cnt[i] + (s[i]=='A'?1:0); rep(i,n+1) cnt[i] = 2*cnt[i] - i; int ans = 0; rep(i,n+1){ if(mp.count(cnt[i]) > 0){ ans = max(ans,i - mp[cnt[i]]); }else{ mp[cnt[i]] = i; } } cout << ans << endl; return 0; }