import std.stdio : writeln, readln, chomp, writefln;
import std.conv : to;
import std.string : split;

int W;
int N;
int treasure[][2];
int ans[2];

void dfs(uint i, int weight, int worth)
{
	if(weight > W){ return; }
	if(ans[0] <= worth){
		if(ans[0] == worth){
			ans[1] = ans[1]<weight ? ans[1] : weight;
		}else{
			ans[0] = worth;
			ans[1] = weight;
		}
	}
	if(i >= N){ return; }
	
	dfs(i+1, weight, worth);
	dfs(i+1, weight + treasure[i][1], worth + treasure[i][0]);
}

void main()
{
	int count = 0;
	while(true){
		ans = [0,0];
		++count;
		//input
		W = to!int(readln().chomp); if(W==0)break;
		N = to!int(readln().chomp);
		
		foreach(i; 0..N){
			string str[] = split(readln(), ",");
			int inf[2];
			foreach(j, ref w; inf){
				w = to!int(str[j].chomp);
			}
			treasure ~= inf;
		}
		//solve
		dfs(0, 0, 0);
		//output
		writefln("Case %d:", count);
		writeln = ans[0];
		writeln = ans[1];
	}
}