#include using namespace std; #define rep(i, n) for(int i = 0; i < int(n); i++) #define REP(a, b, n) for(int i = a; i < b; i++) using lint = long long; signed main(){ string s; cin >> s; int n = s.size(); vector data(n + 1, 0); rep(i, n){ if(s[i] == 'A') data[i + 1] = data[i] + 1; else data[i + 1] = data[i] - 1; } int lim_max = *max_element(data.begin(), data.end()); int lim_min = *min_element(data.begin(), data.end()); vector memo(lim_max + 1 - lim_min, -1); int ans = 0; for(int i = 0; i <= n; i++){ int x = data[i] - lim_min; if(memo[x] == -1) memo[x] = i; else ans = max(ans, i - memo[x]); } cout << ans << endl; }