#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } int n; vector a, b; bool cond(int x){ if(2*x > n) return false; for(int i = 0; i + 2*x <= n; i++){ if((a[i+2*x] - a[i]) == (b[i+2*x] - b[i])) return true; } return false; } // condを満たす最大のxを求める(upper_bound) // 全てのxが条件を満たさない場合は-1を返す(-1はとりえない値) int upper_bound_cond(){ int lb = -1, ub = n; while(ub - lb > 1){ int mid = (lb + ub) / 2; if(cond(mid)) lb = mid; // Answer is in [mid, ub) else ub = mid; // Answer is in [lb, mid) } // now lb + 1 = ub return lb; } int main(){ cin.tie(0); ios::sync_with_stdio(false); string s; cin >> s; n = s.size(); a.resize(n+1, 0); b.resize(n+1, 0); rep(i, n){ a[i+1] = a[i] + (s[i] == 'A' ? 1 : 0); b[i+1] = b[i] + (s[i] == 'B' ? 1 : 0); } cout << max(2 * upper_bound_cond(), 0) << endl; }