#include<cstdio>
#include<algorithm>
using namespace std;

int W,N,c;
int w[1001],p[1001];
int dp[10001][1000];

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]);
		}
		for(int i = 1; i <= N; i++){
			for(int j = 1;j <= W; j++){
				if(i-1 < 0 || j-w[i]<0)continue;
				dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+p[i]);
			}
		}
		c++;
		printf("Case%2d:\n",c);
		printf("%d\n",dp[N][W]);
		for(int i = 0; i < W; i++){
			if(dp[N][W] == dp[N][W-i]){
				ans = i;
			}
		}
		printf("%d\n",W-ans);
		/*ツ渉可甘コツ可サ*/
		for(int i = 0; i < 1001; i++){
			w[i] = 0;
			p[i] = 0;
		}
		for(int i = 0; i < 10001; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
	}
	return 0;
}