#include #include #include #include #include using ll = long long ; using namespace std; int main(){ string str; map m; m[0] = 0; ll ans=0,p=0; cin >> str; ll len = str.size(); for(ll i=1;i<=len;i++){ if(str[i-1]=='A') p++; else p--; if(m.count(p)) ans=(ans,i-m[p]); else m[p]=i; } cout << ans << endl; return 0; }