#include<cstdio>
#include<algorithm>
using namespace std;
int W,N,c;
int w[1024],p[1024],dp[2][10001];

int main(void){
	while(scanf("%d",&W)&&W){
		int ans;
		scanf("%d",&N);
		for(int i = 1; i <= N; i++){
			scanf("%d,%d", &p[i], &w[i]);
		}
		printf("%d %d\n",w[1],p[1]);
		for(int i = 1; i <= N; i++){
			for(int j = 1;j <= W; j++){
				if(j-w[i]<0)continue;
				dp[i%2][j] = max(dp[1-i%2][j],dp[1-i%2][j-w[i]]+p[i]);
			}
		}
		printf("Case%2d:\n",++c);
		printf("%d\n",dp[N%2][W]);
		for(int i = 1; i <= W; i++){
			if(dp[N%2][W] == dp[N%2][i]){
				ans = i;
				break;
			}
		}
		
		printf("%d\n",ans);
		/*ツ渉可甘コツ可サ*/
		for(int i = 0; i < 1024; i++)w[i] = p[i] = 0;
		for(int i = 0; i < 2; i++)for(int j = 0; j < 10001; j++)dp[i][j] = 0;
	}
	return 0;
}