#include using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; int N = S.size(); int n = N + 5; vector> pos(n * 2, vector()); int cum = n; pos[cum].push_back(0); for (int p = 0; p < N; ++p){ if (S[p] == 'A'){ ++cum; }else{ --cum; } pos[cum].push_back(p + 1); } int ans = 0; for (auto &vec: pos){ if (vec.size() > 1){ ans = max(ans, vec[vec.size() - 1] - vec[0]); } } cout << ans << endl; return 0; }