#include<cstdio>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

int w,n;
vector< pair<int,int> > v;

int** GetArray(int w,int h){
	int** pt = new int*[w];
	for(int i=0;i<w;i++){
		pt[i] = new int[h];
		for(int j=0;j<h;j++){
			pt[i][j] = 0;
		}
	}
	return pt;
}

void DeleteArray(int** pt,int w){
	for(int i=0;i<w;i++){
		delete[] pt[i];
	}
	delete[] pt;
}

int main(){
	int setno = 1;
	int a,b;
	while(scanf("%d",&w) && w){
		scanf("%d",&n);
		printf("Case %d:\n",setno);
		v.clear();
		for(int i=0;i<n;i++){
			scanf("%d,%d",&a,&b);
			v.push_back(make_pair(a,b));
		}
		int** dp = GetArray(n,w);
		for(int j=0;j<w;j++){
			//printf("j:%d\n",j);
			dp[0][j] = 0;
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<w;j++){
				//printf("%d:%d\n",i,j);
				if(j < v[i].second){
					dp[i][j] = dp[i-1][j];
				}else{
					dp[i][j] = max(dp[i-1][j] , dp[i-1][j-v[i].second] + v[i].first);
				}
			}
		}
		int ans = dp[n-1][w-1];
		int answ = -1;
		for(int i=0;i<w;i++){
			if(dp[n-1][i] == ans){
				answ = i;
				break;
			}
		}
		printf("%d\n%d\n",ans,answ);
		DeleteArray(dp,n);
		setno++;
	}
}