#include <bits/stdc++.h>
using namespace std;

int main() {
  int W, n;
  for (int Case = 1; cin >> W, W; ++Case) {
    cin >> n;
    vector<int> v(n), w(n);
    for (int i = 0; i < n; ++i) {
      scanf("%d,%d", &v[i], &w[i]);
    }

    vector<int> d(W + 1, -1);
    d[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = W - w[i]; j >= 0; --j) {
        if (d[j] == -1) continue;
        d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]);
      }
    }

    int ans_v = 0, ans_w = 0;
    for (int i = 0; i <= W; ++i) {
      if (ans_v < d[i]) {
        ans_v = d[i];
        ans_w = i;
      }
    }

    cout << "Case " << Case << ":" << endl;
    cout << ans_v << endl;
    cout << ans_w << endl;
  }
}