#include<iostream>
#include<cstdio>
using namespace std;

long long dp[1001][1000];
int main(){
	int w, cntcase = 1;
	while (cin >> w, w){
		int n, treas_val[1000], treas_w[1000], ans_w = 0;
		long long ans_val = 0;
		cin >> n;
		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++){
			scanf("%d,%d", &treas_val[i], &treas_w[i]);
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < w; j++){
				if (dp[i][j] != -1){
					if (j + treas_w[i] < w
						&& dp[i + 1][j + treas_w[i]] < dp[i][j] + treas_val[i])
						dp[i + 1][j + treas_w[i]] = dp[i][j] + treas_val[i];
					if (dp[i][j] > dp[i + 1][j])
						dp[i + 1][j] = dp[i][j];
				}
				//printf("%d ", dp[i][j]);
			}
			//puts("");
		}
		for (int i = 0; i < w; i++){
			if (ans_val < dp[n][i]){
				ans_val = dp[n][i];
				ans_w = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", cntcase, ans_val, ans_w);
		cntcase++;
	}
	return 0;
}