print((function (s) local h,t,m,r = {[0]={0}},0,math.floor(#s/2),0 for i=1,#s do t = s:sub(i,i)=="A" and t+1 or t-1 if t<=m and -m<=t then if h[t] == nil then h[t]={i} else h[t][2]=i end end end for _,v in pairs(h) do if #v==2 and v[2]-v[1]>r then r = v[2]-v[1] end end return r end)(io.stdin:read("*l")))