#include #include #include #include #include #include #include #include #include #include #include #include #include #define p(s) cout<<(s)<=n;i--) #define CK(n,a,b) ((a)<=(n)&&(n)<(b)) #define F first #define S second typedef long long ll; using namespace std; const int inf=1e9+7; string s; int ans,cnt; map m; int main(){ cin>>s; REP(i,0,s.size()){ if(s[i]=='A') cnt++; else cnt--; if(m[cnt]==0){ m[cnt]=i+1; } } RREP(i,0,s.size()){ if(m[cnt]!=0){ ans=max(ans, i+1-m[cnt]); } } p(ans); return 0; }