// 標準入力
var fs = require('fs'),
length = fs.fstatSync(process.stdin.fd).size,
buffer = new Buffer(length),
bytesRead = fs.readSync(process.stdin.fd, buffer, 0, length, 0),
input = buffer.toString('utf8', 0, bytesRead).split('\n');
main();

function main(){
  var read = 0;
  var resultCase = 1;
  while(true){
    var maxWeight = +input[read++];
    if(!maxWeight){
      break;
    }
    
    var treasures = +input[read++];

    // dp[宝][重さ] = 価値
    var dp = new Array(treasures + 1);
    dp[0] = new Int32Array(maxWeight + 1);

    var resultWeight = 0;
    var resultCost = 0;
    for(var i = 1; i <= treasures; i++){
      var dpp = dp[i - 1];
      var dpi = dp[i] = new Int32Array(maxWeight + 1);
      
      var c = input[read++].split(",");
      var tv = +c[0];
      var tw = +c[1];

      // 今のお宝のみ持っていく
      dpi[tw] = tv;

      var dppw;
      for(var w = 1, wlen = maxWeight - tw; w < wlen; w++){
        if(!(dppw = dpp[w]))
          continue;

        var wtw = w + tw;
        var dpiwtw = dpi[wtw] = max(dppw + tv, dpi[wtw]);

        if(dpiwtw > resultCost){
          resultCost = dpiwtw;
          resultWeight = w + tw;
        }
        
      }
      
    }

    console.log("Case " + resultCase++ + ":\n" + resultCost + "\n" + resultWeight);

  }
}

function max(a, b) {
  var t = a - b;
  return a - (t & (t >> 31));
}