#include using namespace std; int mi[500010]; int ma[500010]; int main() { string s; cin >> s; int a[200010] = {}; for (int& x : mi) x = 1 << 29; for (int& x : ma) x = -(1 << 29); mi[200010] = 0; ma[200010] = 0; for (int i = 0; i < s.size(); i++) { if (i) a[i] = a[i - 1]; if (s[i] == 'A') a[i]++; else a[i]--; mi[a[i] + 200010] = min(mi[a[i] + 200010], i + 1); ma[a[i] + 200010] = max(ma[a[i] + 200010], i + 1); } for (int i = 0; i < s.size(); i++) { cerr << a[i] << " " << mi[a[i] + 200010] << " " << ma[a[i] + 200010] << endl; } int ans = 0; for (int i = 0; i < 500010; i++) { if (ma[i] == 1 << 29) continue; ans = max(ans, ma[i] - mi[i]); } cout << ans << endl; return 0; }