#include #include #include #include #include #include #include #include #include #include #include #include typedef long long ll; typedef unsigned long long ull; using namespace std; #define pb push_back int dy[]={0, 0, 1, -1, 0}; int dx[]={1, -1, 0, 0, 0}; #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) for (int i=0;i<(n);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) string s; int a[300000]; deque c[500000]; bool used[500000]; int main(){ cin >> s; ll n = s.length(); REP(i,n) { if(s[i] == 'A') { a[i] = 1; }else{ a[i] = -1; } } REP(i,n - 1) { a[i + 1] += a[i]; } REP(i,n) { c[a[i] + n].push_front(i); } int ans = 0; REP(i,n) { if(!used[a[i] + n]) { used[a[i] + n] = true; ans = max(ans,c[a[i] + n][0] - i); } } cout << ans << endl; return 0; }