#include "bits/stdc++.h" #define fastcin {\ cin.tie(0);\ ios::sync_with_stdio(false);\ } using namespace std; int main() { fastcin; static char S[200000]; cin >> S; int c = 0, a = 0, t = -1; static int l = strlen(S); for(int i = 0; i < l; i++) { if(S[i]=='A') c++; else if(S[i]=='B') c--; if(c==0) { a += (i - t); t = i; } } cout << a << "\n"; return 0; }