#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; int main(){ string s; cin >> s; ll lb = 0, ub = s.size(); while(ub-lb>1){ ll m = (lb+ub)/2; ll sz = 2*m; bool flg = false; if(sz <= s.size()){ ll A = 0; ll B = 0; rep(i,sz){ if(s[i] == 'A') A++; else B++; } if(A == m && B == m) flg = true; REP(i,sz,s.size()){ if(s[i-sz] == 'A') A--; else B--; if(s[i] == 'A') A++; else B++; if(A == m && B == m) flg = true; } } if(flg) lb = m; else ub = m; } cout << 2*lb << endl; return 0; }