#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#define W_MAX 1000
#define N_MAX 1000

int W, N, dp[N_MAX + 1][W_MAX + 1], w[N_MAX + 1], v[N_MAX + 1],c=1;

int main(){
	while (1){
		scanf("%d %d", &W, &N);
		if (W == 0)
			break;
		for (int i = 1; i <= N; ++i){
			scanf("%d,%d", &v[i], &w[i]);
		}
		for (int i = 1; i <= W; ++i)
			dp[i][0] = 0;
		for (int i = 1; i <= W; ++i){
			for (int j = 1; j <= N; ++j){
				if (i < w[j])
					dp[i][j] = dp[i][j - 1];
				else{
					dp[i][j] = std::max(dp[i][j - 1], dp[i - w[j]][j - 1] + v[j]);
				}
			}
		}
		int minv = W;
		for (int i = W; i>=0 ; --i){
			if (dp[W][N] == dp[i][N])
				minv = i;
		}
		printf("%s %d %s \n", "Case", c, ":");
		printf("%d\n", dp[W][N]);
		printf("%d\n", minv);
		++c;
	}
	return 0;
}