#include using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<(n);i++) #define reps(i,f,n) for(int i=(f);i<(n);i++) int main(int argc, char const *argv[]) { string s; cin >> s; vector cnt(s.size() + 1); int now = 0; cnt[0] = 0; rep(i, s.size()) { if (s[i] == 'A') now++; else now--; cnt[i + 1] = now; } map> ma; rep(i, s.size() + 1) { if(ma.count(cnt[i])) { ma[cnt[i]] = make_pair(ma[cnt[i]].first, i); } else { ma[cnt[i]] = make_pair(i, i); } } int ans = 0; for (auto u : ma) { ans = max(ans, u.second.second - u.second.first); } printf("%d\n", ans); return 0; }