S = input() mem = [0] a = b = 0 for c in S: if c == 'A': mem.append(mem[-1] + 1) a += 1 else: mem.append(mem[-1] - 1) b += 1 def judge(n): for a,b in zip(mem, mem[n*2:]): if a == b: return True for a,b in zip(mem[1:], mem[n*2+1:]): if a == b: return True return False ok = 0 ng = min(a,b)+1 while ng-ok > 1: m = (ng+ok) // 2 if judge(m): ok = m else: ng = m print(ok*2)