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

int w, z[1100][2] = {}, map[1100][1100] = {}, n, max_w = 20000000, max_v = 0;

int main(){
	int k = 1;
	while(cin >> w >> n){
		if (w == 0) break;
		max_w = 20000000;
		max_v = 0;
		for(int i = 0;i<n;i++){
			char cmp;
			cin >> z[i][0] >> cmp >> z[i][1];
		}
		for(int i = 1;i<=n;i++){
			for(int j = 0;j<=w;j++){
				if (z[i - 1][1] > j) continue;
				map[i][j] = max(map[i - 1][j], map[i - 1][j - z[i-1][1]] + z[i - 1][0]);
				if(max_v < map[i][j]){
					max_v = map[i][j];
					max_w = j;
				}
				else if(max_v == map[i][j]){
					if (max_w > j) max_w = j;
				}
			}
		}
		cout << "Case " << k << ":" << endl
			<< max_v << endl
			<< max_w << endl;
		k++;
	}
	return 0;
}