#include <iostream>
#include <utility>
#include <algorithm>
#include <cstdio>

#define N_MAX 1000	// ??????????????° ?????§???
#define W_MAX 1000  // ?¢¨?????????????????????????????? ?????§???
#define DIAGONAL 1
#define TOP 0

using namespace std;

int dp[N_MAX+1][W_MAX+1], memo[N_MAX+1][W_MAX+1];
int v[N_MAX+1], w[N_MAX+1];
int N, W;

int calcWeight(int _w) {
	int res = 0;

	for (int i = N; i >= 1; i--) {
		if (memo[i][_w] == DIAGONAL) {
			res += w[i];
			_w -= w[i];
		}
	}

	return res;
}

pair<int, int> knapsack() {
	int ans_w, ans_v, max_w;

	// ?????????
	for (int i = 0; i <= N; i++) {
		for (int j = 0; j <= W; j++) {
			dp[i][j] = 0;
		}
	}

	// ????????¨???
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= W; j++) {
			if (w[i] <= j) {
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);
				memo[i][j] = (dp[i-1][j] < dp[i-1][j-w[i]] + v[i]) ? DIAGONAL : TOP;
			}
			else {
				dp[i][j] = dp[i - 1][j];
				memo[i][j] = TOP;
			}
		}
	}

	// ???????§£?????¢???
	ans_v = -1;
	ans_w = 0;
	for (int i = 1; i <= W; i++) {
		if (ans_v < dp[N][i]) {
			ans_v = dp[N][i];
			max_w = i;
		}
		else if (ans_v == dp[N][i]) {
			if (calcWeight(i) < calcWeight(max_w)) {
				ans_v = dp[N][i];
				max_w = i;
			}
		}
	}

	ans_w = calcWeight(max_w);

	return make_pair(ans_v, ans_w);
}

int main() {
	int cont = 1;
	char c;
	pair<int, int> ans;

	while (cin >> W) {
		if (W == 0)  break;

		cin >> N;

		for (int i = 1; i <= N; i++) {
			cin >> v[i] >> c >> w[i];
		}

		ans = knapsack();

		printf("Case %d:\n%d\n%d\n", cont, ans.first, ans.second);
	}

	return 0;
}