#include "bits/stdc++.h" #define fastcin {\ cin.tie(0);\ ios::sync_with_stdio(false);\ } using namespace std; int main() { fastcin; string S; cin >> S; int l = S.length(), c = 0, ans = 0; map m; m[0] = 0; for(int i = 0; i < l; i++) { if(S[i]=='A') c++; else c--; if(m.find(c)==m.end()) m[c] = i+1; else { if(ans < (i+1 - m[c])) ans = i+1 - m[c]; } } cout << ans << "\n"; return 0; }