lst = list(input()) max_length = 0 count_list = {0:-1} count = 0 for i in range(len(lst)): if lst[i]=="A": count += 1 else: count -= 1 count_list.setdefault(count,i) max_length = max(max_length,i-count_list[count]) print(max_length if max_length > 1 else -1)