def main
  counter = 1
  while (w = gets.to_i) != 0
    arr = []
    gets.to_i.times do
      arr << gets.split(",").map(&:to_i)
    end
    arr.sort_by!{|item|-item[1]}
    arr2 = Array.new(w, 0)
    arr.each{|item|
      arr2[item[1]...-item[1]].each_with_index{|item2, idx2|
        if arr2[idx2] < item2 + item[0]
          arr2[idx2] = item2 + item[0]
        end
      }
      arr2[-item[1]] = item[0]
    }
    max = 0
    maxweight = 0
    arr2.reverse.each_with_index{|item, idx|
      if max < item
        max = item
        maxweight = idx + 1
      end
    }
    puts "Case #{counter}:\n#{max}\n#{maxweight}"
    counter += 1
  end
end
main