#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

void thief(int num,int wei,int total);

int value[1000],weight[1000];
int W,N;
int maxvalue,maxweight;

int main(){
	int ans;
	int Case =1;
	while(cin >> W,W!=0){
		cin>>N;
		maxvalue=0;
		maxweight=1000;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&value[i],&weight[i]);
		}
		thief(0,0,0);
		cout<<"Case "<<Case<<':'<<endl;
		cout<<maxvalue<<endl;
		cout<<maxweight<<endl;
		Case++;
	}
	return 0;
}

void thief(int num,int wei,int total)
{
	if(num==N || wei>W){
		if(maxvalue<=total){
			if(maxvalue == total){
				if(maxweight>wei)
					maxweight=wei;
			}
			else maxweight=wei;
			maxvalue=total;
		}
		return;
	}
	if(wei+weight[num]<=W) thief(num+1,wei+weight[num],total+value[num]);
	thief(num+1,wei,total);
	return;
}