#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int Max(int a,int b){
	if(a>=b)return a;
	else return b;
}

int main(){
	int n,w;

	int cnt = 1;

	while(true){
	cin>>w;
	if(w==0) break;
	cin>>n;
	int *dataW = new int[n];
	int *dataV = new int[n];
	int *ans = new int[w];
	char tmp;
	int num;
	int max = 0;
	int ansW;
	for(int i=0;i<n;i++){
		cin>>num;
		dataV[i] = num;
		cin>>tmp;
		cin>>num;
		dataW[i] = num;
	}
	for(int i=0;i<w;i++){
		ans[i] = -1;
	}
	ans[0] = 0;

	for(int i=0;i<n;i++){
		for(int j=w-1;j>=0;j--){
			if(ans[j] >= 0){
				int tmpW = j + dataW[i];
				if(tmpW < w){
					ans[tmpW] = Max(ans[tmpW],ans[j] + dataV[i]);
				}
			}
		}
		/*
		for(int j=0;j<w;j++) cout<<ans[j]<<" ";
		cout<<endl;
		*/
	}
	for(int i=0;i<w;i++){
		if(max<=ans[i]){
			max = ans[i];
			ansW = i;
		}
	}
	cout<<"Case "<<cnt<<":"<<endl<<max<<endl<<ansW<<endl;
	cnt++;
}
return 0;
}
/*
50
5
60,10
100,20
120,30
210,45
10,4
50
5
60,10
100,20
120,30
210,45
10,4
0
*/