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