(function(input) {
  var p = input.replace(/\n$/, '').split('\n');
  var index = 0;
  while (true) {
    var w = Number(p.shift());
    if (w === 0) {
      return;
    }
    var n = Number(p.shift());
    var ts = [];
    for (var i = 0; i < n; i++) {
      ts.push(p.shift().split(',').map(Number));
    }
    var r = result(0, w, ts, 0);
    var v = r[0];
    var m = r[1];
    console.log('Case %s:', ++index);
    console.log(v);
    console.log(w - m);
  }
})(require('fs').readFileSync('/dev/stdin', 'utf8'));

function result(v, w, ts, i) {
  //console.log(v, w, ts);
  if (ts.length === i) {
    return [v, w];
  }
  var a = ts[i];
  if (a[1] > w) {
    return result(v, w, ts, i + 1);
  }
  return comp(result(v + a[0], w - a[1], ts, i + 1), result(v, w, ts, i + 1))
}

function comp(a, b) {
  if (a[0] > b[0]) {
    return a;
  } else if (a[0] < b[0]) {
    return b;
  } else if (a[1] > b[1]) {
    return a;
  } else {
    return b;
  }
}