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 for n in range(min(a,b),-1,-1): if judge(n): print(n*2) exit()