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