#include #include #include void solve() { std::string s; std::cin >> s; std::map pos; pos[0] = 0; int ans = 0, idx = 0, h = 0; for (char c : s) { h += (c == 'A' ? 1 : -1); ++idx; if (pos.count(h)) { ans = std::max(ans, idx - pos[h]); } else { pos[h] = idx; } } std::cout << ans << std::endl; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }