#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; int lb = 0, ub = s.size()+1; while(ub-lb>1){ int m = (lb+ub)/2; int sz = 2*m; bool flg = false; int A = 0; int B = 0; rep(i,min(sz,(int)s.size())){ 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; }