h,*F = $<.map(&:chomp) puts %w/A B/.map{|c|F.map{|f|f.count(c)}.min}.sum