#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		for (int i = 0;i < n;i++)
			for (int j = 0;j < w;j++)dp[i][j] = -1;
		dp[0][0] = 0;

		for (int i = 0;i < n;i++) {
			for (int j = 0;j <= w;j++) {
				if (dp[i][j] !=-1) {
					dp[i + 1][j] = 0;
					if (j + m[i] <= w)dp[i + 1][j + m[i]] = 0;
					dp[i + 1][j] = dp[i][j];
					dp[i + 1][j + m[i]] = dp[i][j] + v[i];
				}
			}
		}

		int k=0, aw=0;
		for (int i = w;i > -1;i--) {
			if (dp[n][i] >= k) {
				k = dp[n][i];
				aw = i;
			}
		}
		cout << "Case " << x << ":" << endl;
		cout << k << endl << aw << endl;
		x++;
	}
	return 0;
}