#include "bits/stdc++.h"
using namespace std;

struct goods {
	int V;
	int W;
};

int main() {
	int W, COUNT = 1;
	while (cin >> W) {
		if (W == 0) return 0;
		int N;
		char C;
		goods G[1000] = {};
		static long long DP[1010][1010] = {};
		cin >> N;
		for (int i = 0; i < N; i++) cin >> G[i].V >> C >> G[i].W;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				if (j >= G[i].W) DP[i + 1][j] = max(DP[i][j], DP[i][j - G[i].W] + G[i].V);
				else DP[i + 1][j] = DP[i][j];
			}
		}
		long long ANS = DP[N][W];
		for (int i = W; i >= 0; i--) {
			if (ANS != DP[N][i]) {
				cout << "Case " << COUNT << ":" << endl << ANS << endl << i + 1 << endl;
				break;
			}
		}
		COUNT++;
	}
}
