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