#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long MOD = 1000000007; int A[200001*2]; int main() { string S; cin >> S; int N = S.length(); fill( A, A+200001*2, -1 ); int a = 200000; A[200000] = 0; int ans = 0; for ( int i = 1; i <= N; i++ ) { if ( S[i-1] == 'A' ) { a++; } else { a--; } if ( A[a] != -1 ) { ans = max( ans, i-A[a] ); } else { A[a] = i; } } cout << ans << endl; return 0; }