from fractions import gcd from datetime import date from math import factorial import functools from heapq import* from collections import deque import collections import math from collections import defaultdict, Counter import sys sys.setrecursionlimit(10 ** 7) MOD = 10 ** 9 + 7 inf = float("inf") def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) # divisors.sort() return divisors def main(): s = list(input()) n = len(s) l = [0] * (n) if s[0] == "A": l[0] = 1 for i in range(1, n): if s[i] == "A": l[i] = l[i-1] + 1 else: l[i] = l[i - 1] - 1 l = [0] + l d = defaultdict(list) for i in range(n+1): d[l[i]].append(i) ans = 0 for k in d.keys(): if len(d[k]) <= 1: continue ans = max(ans, d[k][-1] - d[k][0]) print(ans) if __name__ == '__main__': main()