#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 sum[200010]; int main(){ cin>>s; m[0]=0; REP(i,0,s.size()){ if(s[i]=='A') cnt++; else cnt--; sum[i+1]=cnt; if(m[cnt]==0&&cnt!=0){ m[cnt]=i+1; } } RREP(i,0,s.size()+1){ ans=max(ans, i-m[sum[i]]); } p(ans); return 0; }