import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.math;

void main(){

    int casenum = 1;
    while(true){
	int w = to!int(chomp(readln()));
	if(w == 0) break;
	int ca = to!int(chomp(readln()));
	int[] trev,trew;
	for(int i=0;i<ca;i++){
	    auto s = split(chomp(readln()),',');
	    trev ~= to!int(s[0]);
	    trew ~= to!int(s[1]);
	}
	int a[1001];
	a[0] = 1;
	int ans=0;
	for(int j=0;j<ca;j++){
	    for(int i = w;i>=0;i--){
		if(i-trew[j]>=0 && a[i-trew[j]] > 0){
		    a[i] = max(a[i],trev[j] + a[i-trew[j]]);
		    ans = max(ans,a[i]);
		}
	    }
	}
	for(int i=0;i<=w;i++){
	    if(a[i] == ans){
		writef("Case %d:\n",casenum);
		writeln(a[i]-1);
		writeln(i);
		break;
	    }
	}
	casenum++;

    }
}