#include <stdio.h>
#include <cmath>
#include <algorithm>

using namespace std;


int main(){
	int W,N,count = 1,max_v,max_w;

	int** dp = new int*[1001];
	for(int i=0; i < 1001; i++){
		dp[i] = new int[1001];
	}

	while(true){
		scanf("%d",&W);
		if(W == 0)break;

		for(int i = 0; i < 1001; i++){
			for(int k=0; k < 1001; k++)dp[i][k] = 0;
		}

		max_v = max_w = -1;

		scanf("%d",&N);

		int v[N],w[N];

		for(int i=0; i < N; i++){
			scanf("%d,%d",&v[i],&w[i]);
		}

		for(int i = N-1; i >= 0; i--){
			for(int k = 0; k <= W; k++){
				if(k < w[i]){
					dp[i][k] = dp[i+1][k];
				}else{
					dp[i][k] = max(dp[i+1][k],dp[i+1][k-w[i]]+v[i]);
				}
			}
		}

		for(int i = W; i >=0; i--){
			if(dp[0][i] >= max_v){
				max_v = dp[0][i];
				max_w = i;
			}
		}

		printf("Case %d:\n%d\n%d\n",count,max_v,max_w);
		count++;
	}


	return 0;
}