h,*F=*$<;p %w/A B/.map{|c|F.map{|f|f.count c}.min}.sum