def main(): import sys input = sys.stdin.readline S = input().rstrip('\n') dic = {0: -1} tmp = 0 ans = 0 for i, s in enumerate(S): if s == 'A': tmp += 1 else: tmp -= 1 if tmp not in dic: dic[tmp] = i else: ans = max(ans, i - dic[tmp]) print(ans) if __name__ == '__main__': main()