S = input() ls = [0] N = len(S) for i in range(N): if S[i] == 'A': ls.append(ls[-1]+1) else: ls.append(ls[-1]-1) ans = 0 lsa = [-1 for i in range(N+1)] for i in range(N+1): if lsa[ls[i]] == -1: lsa[ls[i]] = i else: ans = max(ans,i-lsa[ls[i]]) print(ans)