#include <iostream>
#include <algorithm>
using namespace std;

int W, N;
int v[1000], w[1000], dp[1000+1][1000+1];

int main()
{
  int cnt = 1;
  while (cin >> W) {
    if (W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; ++i) {
      char c;
      cin >> v[i] >> c >> w[i];
    }
    for (int i = 0; i < 1001; ++i) {
      for (int j = 0; j < 1001; ++j)
	dp[i][j] = 0;
    }

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

    value = dp[N][W];
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= W; ++j) {
	if (value == dp[i][j])
	  weight = min(weight, j);
      }
    }

    cout << "Case " << cnt++ << ":" << endl << value << endl << weight << endl;
  }
  return 0;
}