#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
	int c = 1, W;

	while(cin >> W, W) {
		int n, mx = 0, w[1000], d[1000];
		long dp[1001] = {0};
		cin >> n;
		for(int i = 0; i < n; i++)
			scanf("%d,%d", &d[i], &w[i]);
		for(int i = 1; i <= W; i++)
			dp[i] = -1;

		for(int i = 0; i < n; i++) {
			for(int j = W - w[i]; j >= 0; j--) {
				if(dp[j] >= 0 && j + w[i] <= W) {
					dp[j + w[i]] = ((dp[j + w[i]] > dp[j] + d[i]) ? dp[j + w[i]] : (dp[j] + d[i]));
				}
			}
		}

		for(int i = 0; i <= W; i++)
			if(dp[i] > dp[mx]) mx = i;

		cout << "Case " << c++ << endl;
		cout << dp[mx] << endl;
		cout << mx << endl;
	}

	return 0;
}