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

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

int solve(int i, int weight, int value)
{
  if (dp[i][weight] != -1)
    return dp[i][weight];

  dp[i][weight] = value;
  if (i == N)
    return value;

  int ret = -1;
  if (weight+w[i] <= W)
    ret = max(ret, solve(i+1, weight+w[i], value+v[i]));
  return max(ret, solve(i+1, weight, value));  
}

int main()
{
  int cnt = 1;
  while (~scanf("%d", &W)) {
    if (W == 0)
      break;

    for (int i = 0; i < 1001; ++i) {
      for (int j = 0; j < 1001; ++j)
	dp[i][j] = -1;
    }

    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", &v[i], &w[i]);

    int value = solve(0, 0, 0), weight = 1 << 30;
    for (int i = 0; i < N+1; ++i) {
      for (int j = 0; j < W+1; ++j) {
	if (dp[i][j] == value)
	  weight = min(weight, j);
      }
    }
    printf("Case %d:\n", cnt++);
    printf("%d\n%d\n", value, weight);
  }
  return 0;
}