S = input() listS = list(S) listN = [] listA = [] listMAX = {} listMIN = {} mx = 2 count = 0 for a in listS: if a == "A": count += 1 listN.append(count) if a == "B": count -= 1 listN.append(count) if listA.count(count) ==0: listA.append(count) for a in listA: listMIN[a] = listN.index(a); for d,e in enumerate(listN): listMAX[e] = d; for a in listA: if listMAX[a] - listMIN[a] > mx: mx = listMAX[a] - listMIN[a] if len(S) !=1: print(str(mx) + "\n") else: print("0\n")