s=gets.chomp n=s.size a_num=s.count(?A) b_num=n-a_num if a_num==b_num puts n exit end if a_num==0||b_num==0 puts 0 exit end min_char,max_char= a_numsize*2 d1=min_char_pos[1]-min_char_pos[0] d2=min_char_pos[-1]-min_char_pos[-2] if d1>d2 min_char_pos.shift else min_char_pos.pop end else puts size*2 exit end end