#include using namespace std; using ll = long long; template using vt = vector; template using vvt = vector>; template using ttt = tuple; using tii = tuple; using vi = vector; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define pb push_back #define mt make_tuple #define ALL(a) (a).begin(),(a).end() #define FST first #define SEC second #define DEB cerr<<"!"<0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;} inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;} // IT 5000兆 欲しい /* Coding space */ template struct FT{ vector d; FT(size_t n):d(n+1){} T sum(size_t i){T s{};for(++i;i>0;i-=i&-i)s+=d[i];return s;} T sum(size_t i,size_t j){return i?sum(j)-sum(i-1):sum(j);} void add(size_t i,T x){for(++i;i ft[2] = {300000,300000}; string str; bool check(int x){ x *= 2; for(int i = 0; i + x <= (int)str.size(); i++){ if(ft[0].sum(i,i + x - 1) == ft[1].sum(i,i + x - 1)) return false; } return true; } int main(){ cin >> str; rep(i,str.size()){ if(str[i] == 'A') ft[0].add(i, 1); else ft[1].add(i, 1); } int top = str.size()/2; int bottom = 0; while(abs(top - bottom) > 1){ int mid = (top + bottom) /2; if(check(mid)) top = mid; else bottom = mid; } if(check(bottom) == 0) bottom--; cout << bottom*2 << endl; }