S = input() cumsum = [0] for i,s in enumerate(S): if s == "A": cumsum.append(cumsum[i]+1) else: cumsum.append(cumsum[i]-1) #print(cumsum) d = {} for i, v in enumerate(cumsum): if v not in d: d[v] = [i,0] else: d[v][1] = i ans = 0 for v in d.values(): ans = max(ans, v[1]- v[0]) print(ans)