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