S=input() L = len(S) d = {} d[0] = -1 cnt = 0 ans = 0 for i in range(L): if S[i]=='A': cnt += 1 else: cnt -=1 if cnt not in d: # キーが存在しない場合の処理 d[cnt] = i else: ans = max(ans,i-d[cnt]) print(ans)