
import std.stdio,std.string,std.conv;

int dp[1001][1001];

int main()
{
	int W,n,ans;
	int w[1001];
	int v[1001];
	int count = 0;
	while((W = readln.chomp.to!int) != 0)
	{
		count++;
		n = readln.chomp.to!int;

		foreach(l;0..1001)
		{
			foreach(k;0..1001)
			{
				dp[l][k] = 0;
			}
		}

		foreach(i;0..n)
		{
			string[] s = readln.chomp.split(",");
			v[i] = s[0].to!int;
			w[i] = s[1].to!int;
		}
		foreach(i;0..n)
		{
			foreach(j;0..W+1)
			{
				if(j < w[i])
				{
					dp[i+1][j] = dp[i][j];
				}
				else
				{
					if(dp[i][j] > dp[i][j-w[i]] + v[i])
					{
						dp[i+1][j] = dp[i][j];
					}
					else
					{
						dp[i+1][j] = dp[i][j-w[i]] + v[i];
					}
				}
			}
		}

		foreach(j;0..W+1)
		{
			if(dp[n][j] == dp[n][W])
			{
				ans = j;
				break;
			}
		}

		writeln("Case ",count,":");
		dp[n][W].writeln;
		ans.writeln;
	}
	return 0;
}