#include using namespace std; #define REP(i, n) for (int i = 0; i < n; i++) int main() { string s; cin >> s; map mp; int ans = 0, cnt = 0; mp[0] = -1; REP(i,s.size()){ cnt += s[i]=='A'?1:-1; if(!mp.count(cnt))mp[cnt] = i; ans = max(ans,i-mp[cnt]); } cout << ans << endl; return 0; }