#include <iostream>
#include <string.h>
#include <vector>
#include <cstdio>

#define W 1010
#define N 1010

using namespace std;

int main(void)
{
	int w,n;
	int i1,i2;
	int dp[N][W];
	int v[N];
	int ww[N];
	int c=0;
	int maxx;
	int i;

	while(1)
	{
		cin>>w;
		if(w==0) break;
		cin>>n;

		for(i1=0;i1<n;i1++){
			scanf("%d,%d",v+i1,ww+i1);
		}
		memset(dp,0,sizeof(dp));

		for(i1=n-1;i1>=0;i1--){
			for(i2=0;i2<=w;i2++){

				if(ww[i1]>i2) dp[i1][i2]=dp[i1+1][i2];
				else {
					dp[i1][i2]=max(dp[i1+1][i2],dp[i1+1][i2-ww[i1]]+v[i1]);
				}
			}
		}
		c++;
		cout<<"Case "<<c<<":"<<endl;
		cout<<dp[0][w]<<endl;
		
		maxx=dp[0][0];
		for(i1=0;i1<=w;i1++){
			if(maxx<dp[0][i1]) {
				i=i1;
				maxx=dp[0][i1];
			}
		}

		cout<<i<<endl;
	}

	return 0;
}