import std.stdio;
import std.array;
import std.conv;
import std.algorithm;
import std.string;
import std.numeric;
import std.range;
void main() {
	for(int count = 1; ; count++){
		auto w = readln.strip.to!int;
		if(w==0) break;
		auto n = readln.strip.to!int;
		auto wv = n.iota.map!(_ => readln.strip.split(',').map!(to!int).array).array;
		int[][] dp = new int[][](n+1,w+1);
		for(int i=n-1;i>=0;i--){
			for(int j=0;j<=w;j++){
				auto cv = wv[i][0];
				auto cw = wv[i][1];
				if(j<cw) dp[i][j] = dp[i+1][j];
				else dp[i][j] = max(dp[i+1][j], dp[i+1][j-cw]+cv);
			}
		}
		writefln("Case %d:", count);
		writeln(dp[0][w]);
		for(int j=w;j>0;j--){
			if(dp[0][w] != dp[0][j]){
				writeln(j+1);
				break;
			}
		}
	}
}