#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;

int main(int argc, char *argv[]) {
  int n, b, loop = 1;
  while (cin >> b, b) {
    cin >> n;
    vector<int> wv(n, 0), vv(n, 0);
    for (int i = 0; i < n; i++) {
      int w, v;
      char c;
      cin >> v >> c >> w;
      wv[i] = w;
      vv[i] = v;
    }

    vector<int> res(b + 1, -1), log(b + 1, -1);
    res[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = b - wv[i]; j >= 0; j--) {
        if (res[j] >= 0 && res[j] + vv[i] > res[j + wv[i]]) {
          res[j + wv[i]] = res[j] + vv[i];
          log[j + wv[i]] = i;
        }
      }
    }

    auto itr = max_element(res.begin(), res.end());

    cout << "Case " << loop << ':' << endl;
    cout << *itr << endl;
    cout << itr - res.begin() << endl;
    loop++;
  }
  return 0;
}