#include using namespace std; #define REP(i,n) for(int i=0; i<(n); i++) #define REP2(i,x,n) for(int i=x; i<(n); i++) #define ALL(n) begin(n),end(n) struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; const long long INF = numeric_limits::max(); string S; int res{}; bool dfs( int i, string T ) { if( count( ALL( T ), 'A' ) == count( ALL( T ), 'B' ) ) { res = max( res, (int)T.size() ); } if( i == (int)S.size() - 1 ) { return 0; } return dfs( i + 1, T ) || dfs( i + 1, T += S[ i + 1 ] ); } int main() { cin >> S; dfs( 0, "" ); cout << res << endl; return 0; }