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

int dp[1001][1001];

int main(){
	int case_number = 1;
	while(1){
		int W, N;
		scanf("%d%d", &W, &N);
		if(W == 0) break;

		int v, w;
		for(int i = 1; i <= N; ++i){
			scanf("%d,%d", &v, &w);
			for(int j = 0; j <= W; ++j){
				if(j < w){
					dp[i][j] = dp[i-1][j];
				}
				else{
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w] + v);
				}
			}
		}

		int answ = 0;
		for(int j = 0; j <= W; ++j){
			if(dp[N][j] > dp[N][answ]){
				answ = j;
			}
		}

		printf("Case %d:\n%d\n%d\n", case_number, dp[N][answ], answ);
		++case_number;
	}
}