S = [1 if x == "A" else -1 for x in input()] left = {} ans = 0 x = 0 for i, s in enumerate(S, 1): x += s if x in left: ans = max(ans, i - left[x]) else: left[x] = i print(ans)