S = input() if len(S) < 2 or S.count('A') == 0 or S.count('B') == 0: print('0') else: end = False for cutLength in range(2,len(S)+1)[::-1]: for startIndex in range(0,len(S)-cutLength+1): if S[startIndex:startIndex+cutLength].count('A') == S[startIndex:startIndex+cutLength].count('B'): print(cutLength) end = True break if end: break