import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex;
import std.container;

void main()
{
	int w, cnt = 1;

	while (w = readln.chomp.to!int, w) {
		auto n = readln.chomp.to!int;
		auto dp = new int[][](n+1, w+1);
		foreach(i; 0..n+1) {
			dp[i][] = -1;
		}
		dp[0][0] = 0;

		foreach (i; 1..n+1) {
			auto tre = readln.chomp.split(",").map!(to!int);
			foreach (j; 0..w+1) {
				if (dp[i-1][j] == -1) continue;
				if (j + tre[1] <= w) {
					dp[i][j+tre[1]] = max(dp[i][j+tre[1]], dp[i-1][j] + tre[0]);
				}
				dp[i][j] = max(dp[i-1][j], dp[i][j]);
			}
		}

		int index, res = -1;
		foreach (i; 0..w+1) {
			if (dp[n][i] > res) {
				res = dp[n][i];
				index = i;
			}
		}
		writeln("Case ", cnt++, ":");
		writeln(res);
		writeln(index);
	}
}