#include <iostream>
#include <string>
#include <vector>
using namespace std;

int wmax, n;
vector<int> v(1000), w(1000);

int main() {
  int cnt = 0;
  while (cin >> wmax >> n) {
    for (int i = 0; i < n; ++i) {
      string tmp;
      getline(cin, tmp, ',');
      v[i] = atoi(tmp.c_str());
      getline(cin, tmp);
      w[i] = atoi(tmp.c_str());
    }

    vector<vector<int>> res(wmax+1, vector<int>(n));

    for (int c=0; c<n; ++c) {
      for (int r=0; r<wmax+1; ++r) {
        if (c==0) {
          res[r][c] = (r < w[0]) ? 0 : v[0];
        }
        else {
          if (r < w[c]) {
            res[r][c] = res[r][c-1];
          }
          else {
            int v1 = res[r][c-1];
            int v2 = res[r-w[c]][c-1] + v[c];
            res[r][c] = (v1 < v2)? v2 : v1;
          }
        }
      }
    }

    int res_v = 0;
    int res_w = wmax;
    for (int r=0; r<wmax+1; ++r) {
      for (int c=0; c<n; ++c) {
        if (res_v < res[r][c]) {
          res_v = res[r][c];
          res_w = r;
        }
      }
    }

    cout << "Case " << ++cnt << ":" << endl;
    cout << res_v << endl;
    cout << res_w << endl;
  }
  return EXIT_SUCCESS;
}

