#include<cstdio>
#include<cstring>
int W;
int N;
int C[1000];
int G[1000];
int dpc[1000][1000]={};
int dpw[1000][1000]={};
int roop=0;

int min(int a,int b){
	if(a<b) return a;
	else return b;
}
int max(int a,int b){
	if(a>b) return a;
	else return b;
}

int dfsc(int k,int c){
	int res;
	if(dpc[k][c]>-1)
		res=dpc[k][c];
	else if(k>N) res=0;
	else if(c+G[k]<W)
		res=max(dfsc(k+1,c+G[k])+C[k],dfsc(k+1,c));
	else
		res=dfsc(k+1,c);
	dpc[k][c]=res;
	return res;
}
int dfsw(int k,int c){
	int res;
	if(dpw[k][c]>-1)
		res=dpw[k][c];
	else if(k>N) res=0;
	else if(c+G[k]<W)
		res=max(dfsw(k+1,c+G[k])+G[k],dfsw(k+1,c));
	else
		res=dfsw(k+1,c);
	dpw[k][c]=res;
	return res;
}


int main(){
	while(1){
		roop++;
	memset(dpc,-1,sizeof(dpc));
	memset(dpw,-1,sizeof(dpw));
	scanf("%d",&W);
	if(W==0) break;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d,%d",&C[i],&G[i]);
	}
	int ansc=dfsc(0,0);
	int answ=dfsw(0,0);
	printf("Case %d:\n%d\n%d\n",roop,ansc,answ);
	}
return 0;
}