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

vector<int> split(const string &str, char sep) {
  vector<int> v;
  stringstream ss(str);
  string buffer;
  while( getline(ss, buffer, sep) ) {
    v.push_back(stoi(buffer));
  }
  return v;
}

int main() {
  int W;
  int cnt = 0;
  while (cin >> W, W) {
    int N; cin >> N;
    cout << "Case " << ++cnt << ":" << endl;
    auto v = vector<int>(N);
    auto w = vector<int>(N);
    for (int i = 0; i < N; ++i) {
      string s; cin >> s;
      auto tmp = split(s, ',');
      v[i] = tmp[0];
      w[i] = tmp[1];
    }
    auto dp = vector<vector<int>>(N + 1, vector<int>(W + 1, -1));
    auto dp2 = vector<vector<int>>(N + 1, vector<int>(W + 1, 0));
    for (int j = 0; j < W + 1; ++j) dp[0][j] = 0;
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < W + 1; ++j) {
        if (j < w[i]) {
          dp[i + 1][j] = dp[i][j];
          dp2[i + 1][j] = dp2[i][j];
        } else {
          if (dp[i][j] < dp[i][j - w[i]] + v[i]) {
            dp2[i + 1][j] = dp2[i][j - w[i]] + w[i];
          } else {
            dp2[i + 1][j] = dp2[i][j];
          }
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
        }
      }
    }
    int m = dp2[N][W];
    for (int j = W - 1; j >= 0; --j) {
      if (dp[N][j] < dp[N][W]) break;
      m = min(m, dp2[N][j]);
    }
    cout << dp[N][W] << endl;
    cout << m << endl;
  }
  return 0;
}

