def AB(S): oriS = S S = list(S) N = len(S) ans = 0 i = 0 while i <= N - 2: patS = S[i:i+2] if (patS == ['A', 'B']) or (patS == ['B', 'A']): S.pop(i) S.pop(i) N -= 2 else: i += 1 resS = '' for j in range(len(S)): resS += S[j] resN = len(resS) oriN = len(oriS) if (oriS[0] == resS[0]) or (oriS[-1] == resS[0]): if (oriS[:resN] == resS) or (oriS[oriN-resN:] == resS): ans = oriN - resN else: splS = resS[0] + resS + resS[0] splitted_S = oriS.split(splS) len1 = len(splitted_S[0]) len2 = len(splitted_S[1]) len_max = max([len1, len2]) ans = len_max + 1 return ans def main(): S = input() print(AB(S)) if __name__ == '__main__': main()