S = gets.strip L = S.size occurrences = Array.new( L * 2 + 2 ) { Array.new } occurrences[L] << 0 depth = 0 L.times do |i| depth += if S[i] == 'A' then 1 else -1 end occurrences[ L + depth ] << i + 1 end res = 0 occurrences.each do |ary| if !ary.empty? then res = [ res, ary.last - ary.first ].max end end puts res