#include using namespace std; #define int long long signed main() { string s; cin >> s; int N = s.size(); map m; int p = 0; m[0] = -1; int ans = 0; for (int i = 0; i < N; i++) { char c = s[i]; if (c == 'A') p++; else p--; if (m[p]) ans = max(ans, i - m[p]); if (!m[p]) m[p] = i; } cout << ans << endl; return 0; }