#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int main(void)
{
	int w,n;
	int i,j;
	int cnt=0;
	int value,weight;
	int vsum,wsum;
	char ch;
	while(1){
		cnt++;
		cin>>w;
		if(w==0){
			break;
		}
		cin>>n;
		
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				dp[i][j]=0;
			}
		}
		for(i=0;i<n;i++){
			cin>>value>>ch>>weight;
			for(j=0;j<=w;j++){
				if(dp[i+1][j]<dp[i][j]){
					dp[i+1][j]=dp[i][j];
				}
				if(j+weight<=w&&dp[i+1][j+weight]<dp[i][j]+value){
					dp[i+1][j+weight]=dp[i][j]+value;
				}
			}
		}
		vsum=-1;
		for(j=0;j<=w;j++){
			if(vsum<dp[n][j]){
				vsum=dp[n][j];
				wsum=j;
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<vsum<<endl<<wsum<<endl;
	}
	return 0;
}
