local ffi = require 'ffi' print((function (s, ct) local h,t,r = {[0]=ct(0,-1)},0,0 local m = math.floor(#s/2) 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]=ct(i,-1) else h[t].r=i end end end for _,v in pairs(h) do if v.r~=-1 and v.r-v.l>r then r=v.r-v.l end end return r end)(io.stdin:read("*l"), ffi.typeof("struct { int l; int r; }")))