S = input() cumsum = [None]*(len(S)+1) cumsum[0] = 1 for i in range(1,len(S)+1): if S[i-1] == 'A': cumsum[i] = cumsum[i-1] + 1 else: cumsum[i] = cumsum[i-1] - 1 begin_point = {} for i,s in enumerate(cumsum): if s not in begin_point: begin_point[s] = [i,-1] else: begin_point[s][1] = i ans = 0 for val in begin_point.values(): ans = max(ans,val[1]-val[0]) print(ans)