#include using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; int n = int(s.size()); vectora(n, 0), b(n, 0); if (s[0] == 'A')a[0] = 1; else a[0] = -1; for (int i = 1; i < n; i++) { if (s[i] == 'A')a[i] = a[i - 1] + 1; else a[i] = a[i - 1] - 1; } int ans = 0; mapm; for (int i = 0; i < int(s.size()); i++) { if (m[a[i]] == 0) { m[a[i]] = i; } else { ans = max(ans, i - m[a[i]]); } } cout << ans << endl; }