N, M = gets.split.map(&:to_i) S = gets.chomp T = gets.chomp counter_a = S.chars.tally counter_b = T.chars.tally counter_a.default = 0 counter_b.default = 0 puts [counter_a['A'], counter_b['A']].min + [counter_a['B'], counter_b['B']].min