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