#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_element(data.begin(), data.end()); int ans = 0; for(int i = 0; i <= lim; i++){ int a = 0, b = 0; rep(j, n){ if(data[j] == i){ a = j; break; } } for(int j = n - 1; j >= 0; j--){ if(data[j] == i){ b = j; break; } } ans = max(ans, b - a); } cout << ans << endl; }