#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int main(void) {
	int W, N, i, j = 1, index, maxValue, minWeight;
	int result[1001], weight[1001], value[1001];
	char c;

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

		for (i = 0; i < 1001; i++) {
			value[i] = weight[i] = result[i] = 0;
		}

		cin >> N;
		for (i = 0; i < N; i++) {
			cin >> value[i] >> c >> weight[i];
		}
		
		for (i = 0; i < N; i++) {
			for (index = W; index >= weight[i]; index--) {
				result[index] = max(result[index], result[index - weight[i]] + value[i]);
			}
		}

		maxValue = result[W];
		for (i = 0; i < 1001; i++) {
			if (result[i] == maxValue) {
				minWeight = i;
				break;
			}
		}

		cout << "Case " << j << ":" << endl << maxValue << endl << minWeight << endl;
		j++;
	}

	return 0;
}


