# 二重ループはできない, 2*10**5 # 入力例1,AAABBA。+1 for A, -1 for Bとすれば # levels = [0, 1, 2, 3, 2, 1, 2] # 同じ数字で一番離れている区間が答え S = input() levels = [0] for s in S: if s == 'A': levels.append(levels[-1]+1) if s == 'B': levels.append(levels[-1]-1) #print(levels) from collections import defaultdict dic = defaultdict(list) for i in range(len(levels)): dic[levels[i]].append(i) #print(dic) ans = 0 for d in dic: if len(dic[d]) >= 2: calc = dic[d][-1]-dic[d][0] ans = max(ans, calc) print(ans)