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

#define rep(i,n) for(int i=0; i<n; i++)
int dp[1001][1001];

int main(){
	int W, cnt=1;
	while( scanf("%d",&W), W ){
		int n, v[1000], w[1000];
		memset( dp, -1, sizeof(dp) );
		scanf("%d", &n);
		rep(i,n){
			scanf("%d,%d", &v[i], &w[i]);
		}
		dp[0][0] = 0;
		rep(i,n){
			for(int j=W; j>=w[i]; j--){
				if( dp[i][j-w[i]] != -1 ){
					dp[i+1][j] = max( dp[i][j-w[i]]+v[i], dp[i][j] );
				}
			}
			for(int j=w[i]-1; j>=0; j--){
				dp[i+1][j] = dp[i][j];
			}
		}
		int mw = 0;
		rep(i,W){
			if( dp[n][i]>dp[n][mw] )	mw = i;
		}
		printf("Case %d:\n",cnt++);
		printf("%d\n", dp[n][mw]);
		printf("%d\n", mw);
	}
}