#include <iostream>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

#define MAX_N 1000
#define MAX_W 1000

int W, N, c, v[MAX_N], w[MAX_N], dp[MAX_N + 1][MAX_W + 1];

int main()
{
	while (true)
	{
		c++;

		cin >> W;

		if (!W) { break; }

		cin >> N;

		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}

		for (int j = 0; j <= MAX_W; j++)
		{
			dp[0][j] = 0;
		}

		for (int i = 1; i <= MAX_N; i++)
		{
			for (int j = 0; j <= MAX_W; j++)
			{
				dp[i][j] = -(1 << 28);
			}
		}

		for (int i = 1; i <= N; i++)
		{
			for (int j = 0; j <= W; j++)
			{
				dp[i][j] = max(dp[i - 1][j], j >= w[i] ? dp[i - 1][j - w[i]] + v[i] : 0);
			}
		}

		cout << "Case " << c << ':' << endl;
		cout << dp[N][W] << endl;
		cout << distance(dp[N], lower_bound(dp[N], dp[N] + W + 1, dp[N][W])) << endl;
	}

	return 0;
}