#include <bits/stdc++.h>

using namespace std;

int dp[1001][1001];

int main(void) {
  int W, n;
  int num = 1;
  while (cin >> W, W) {
    cin >> n;
    memset(dp, 0, sizeof(dp));
    
    vector<int> v(n), w(n);
    char c;
    for (int i = 0; i < n; i++) {
      cin >> v[i] >> c >> w[i];
    }

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

    printf("Case %d:\n", num++);
    cout << dp[n][W] << endl;
    cout << lower_bound(dp[n], dp[n] + W, dp[n][W]) - dp[n] << endl;
  }

  return 0;
}
