#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){
			rep(j,W+1){
				if( j<w[i] )
					dp[i+1][j] = dp[i][j];
				else
					dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]]+v[i] );
			}
		}
		int mw = 0;
		rep(i,W){
			if( dp[n][i]>dp[n][mw] )	mw = i;
		}
		printf("Case %d:\n%d\n%d\n", cnt++, dp[n][mw], mw);
	}
}