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

using namespace std;

int main() {
	int n, W;
	char dummy;
	int count = 1;

	while( true ) {

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

		vector< int > item_val(n);
		vector< int > item_wei(n);
		vector< vector< int > > nap(n + 1, vector< int >(W + 1, 0));

		for(int i = 0; i < n; i++) {
			cin >> item_val[i] >> dummy >> item_wei[i];
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= W; j++) {
				if(j + item_wei[i] <= W) {
					nap[i + 1][j + item_wei[i]] = max(nap[i + 1][j + item_wei[i]], nap[i][j] + item_val[i]);
				}
				nap[i + 1][j] = max(nap[i + 1][j], nap[i][j]);
			}
		}

		int max_weight = W;
		int max_value = 0;
		for(int j = 0; j <= W; j++) {
			if(max_value < nap[n][j]) {
				max_value = nap[n][j];
				max_weight = j;
			}
		}

		cout << "Case " << count << ":" << endl;
		cout << max_value << "\n" << max_weight << endl;
		count++;

	}
}