S = input() from collections import defaultdict d = defaultdict(int) now = 0 ans = 0 d[now] = -1 for i in range(len(S)): if S[i] == "A": now += 1 else: now -= 1 if now in d: if i - d[now] > ans: ans = i - d[now] else: d[now] = i print(ans)