#include<iostream>
#include<algorithm>

using namespace std;




int main(){
	int dp[1001];
	int i, j;
	int Mw,n,v,w,t=1;
	char gomi;
	int saidai;


	while (1){

		fill(dp, dp + 1000, 0);
		cin >> Mw;
		if (Mw == 0){
			break;
		}
		cin >> n;
		for (i = 1; i <= n; i++){
			cin >> v;
			cin >> gomi;
			cin >> w;
			if (dp[w] < v){
				dp[w] = v;
				for (j = w-1; j >= 1; j--){
					if (w + j>1001){
						continue;
					}
					if (dp[j] == 0){
						continue;
					}
					dp[w + j] = max(dp[w] + dp[j], dp[w + j]);
				}
			}
		}

		for (i = Mw,saidai=0; i>=0; i--){
			if (dp[saidai] <= dp[i]){
				saidai = i;
			}
		}


		cout << "Case " << t << ":" << endl;
		cout << dp[saidai] << endl;
		cout << saidai << endl;
		t++;


	}

}