#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define llong long long int main() { string s; cin >> s; vector pos(2*s.length()+1, s.length()); pos[s.length()] = 0; int index = s.length(); int result = 0; rep(i, s.length()) { if(s[i] == 'A') index++; else index--; result = max(result, i+1-pos[index]); pos[index] = min(pos[index], i+1); } cout << result << "\n"; }