until (n = $<.gets.to_i).zero?
  m = $<.gets.to_i
  field = {0=>0}
  Array.new(m) {$<.gets.split(",").map(&:to_i)}.each do |a, b, d|
    n = d / 100 - 1
    added = {}
    field.each do |spots, num|
      spots |= 2 ** a
      spots |= 2 ** b
      added[spots] = num + n if !added[spots] or num + n < added[spots]
    end
    field.merge!(added) {|spots, b, f| [b, f].min}
  end
  puts field[2 ** n - 1]
end
