#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main() {
	int W, N;
	char d;
	int counter = 1;
	int takara[1000][2];
	while(1) {
		cin >> W;
		if (W == 0) {
			break;
		}
		cin >> N;
		for (int i = 0; i < N; i++) {
			cin >> takara[i][0] >> d >> takara[i][1];
		}
		vector< vector<int> > dp(W, vector<int> (N+1));
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < W; j++) {
				dp[j][i] = 0;
			}
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < W; j++) {
				if (i == 0) {
					continue;
				}
				if (j < takara[i-1][1]) {
					dp[j][i] = dp[j][i-1];
				} else {
					dp[j][i] = max(dp[j][i-1], dp[j-takara[i-1][1]][i-1] + takara[i-1][0]);
				}
			}
		}
		int MAX[2];
		MAX[0] = 0;
		MAX[1] = 10000;
		for (int i = 0; i < W; i++) {
			if (MAX[0] == dp[i][N]) {
				MAX[1] = min(MAX[1],i);
			} else if (MAX[0] < dp[i][N]){
				MAX[0] = dp[i][N];
				MAX[1] = i;
			}
		}
		cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl;
		counter++;
	}
	return 0;
}