#include using namespace std; #define DBG cerr << '!' << endl; #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i) #define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i)) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;} #define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed< P; int num[222222]; bool ok[222222]; int main() { string str; cin >> str; int a = 0; REP(i,str.size()){ if(i != 0){ num[i] = num[i-1]; } if(str[i] == 'A'){ a++; num[i]++; ok[i] = true; } } // SHOW1d(num,str.size()); // SHOW1d(ok,str.size()); for(int i = min(a,((int)str.size() - a));i > 0;i--){ // cout << i << endl; for(int j = 2*i-1;j < str.size();j++){ // cout << j << ' ' << j-2*i+1 << endl; if((num[j]-num[j-2*i+1] == i&&!(ok[j - 2*i+1]))||(num[j] - num[j - 2*i+1] == i-1 && ok[j - 2*i+1])){ cout << 2*i << endl; return 0; } } } cout << 0 << endl; return 0; }