#include <stdio.h>

typedef struct{
	int sum_v;
	int sum_w;
}tre;

int max_w, n, v[1000], w[1000];
int sum_w;
tre memo[1001][1001];

tre dp(int i, int j);

int main(void){
	int num = 1;
	int i, j;
	tre treasure;

	while(1){
		scanf("%d", &max_w);
		if(max_w == 0) break;
		scanf("%d", &n);
		for(i=0; i<n; i++){
			scanf("%d,%d", &v[i], &w[i]);
		}

		for(i=0; i<1001; i++){
			for(j=0; j<1001; j++){
				memo[i][j].sum_v = -1;
				memo[i][j].sum_w = -1;
			}
		}

		treasure = dp(0, max_w);
		printf("Case %d:\n", num);
		printf("%d\n", treasure.sum_v);
		printf("%d\n", treasure.sum_w);
		num++;
	}

	return 0;
}

tre dp(int i, int j){
	if((memo[i][j]).sum_v >= 0){
		return memo[i][j];
	}

	tre t;
	tre temp_1, temp_2;
	int in, notin;
	if(i == n){
		t.sum_v = 0;
		t.sum_w = 0;
	}else if(j < w[i]){
		t.sum_v = dp(i+1, j).sum_v;
	}else{
		temp_1 = dp(i+1, j);
		temp_2 = dp(i+1, j-w[i]);

		notin = temp_1.sum_v;
		in = temp_2.sum_v + v[i];

		if(temp_2.sum_v + v[i] > temp_1.sum_v){
			t.sum_v = temp_2.sum_v + v[i];
			t.sum_w = temp_2.sum_w + w[i];
		}else if(temp_2.sum_v + v[i] < temp_1.sum_v){
			t.sum_v = temp_1.sum_v;
			t.sum_w = temp_1.sum_w;
		}else{
			if(temp_2.sum_w + w[i] > temp_1.sum_w){
				t.sum_v = temp_1.sum_v;
				t.sum_w = temp_1.sum_w;
			}else{
				t.sum_v = temp_2.sum_v + v[i];
				t.sum_w = temp_2.sum_w + w[i];
			}
		}
	}
	return memo[i][j] = t;
}