#include <iostream>
#include <vector>

using namespace std;

void solve()
{
	int W;
	int N;
	int count = 0;
	while(cin >> W, W)
	{
		count++;
		cin >> N;
		vector<int> Value(N);
		vector<int> Weight(N);
		for(int i = 0; i < N; ++i)
		{
			scanf("%d,%d", &Value[i], &Weight[i]);
		}
		int maxVal = 0;
		vector< vector<int> > dp(N + 1, vector<int>(W + 1));
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j < W; ++j)
			{
				if(j < Weight[i])				
				{
					dp[i + 1][j] = dp[i][j];
				}
				else
				{
					dp[i + 1][j] = max(dp[i][j], dp[i][j - Weight[i]] + Value[i]);
					if(maxVal < dp[i + 1][j])
					{
						maxVal = dp[i + 1][j];
					}
				}
			}
		}
		int index = 0;
		for(int i = 0; i < W + 1; ++i)
		{
			for(int j = 0; j < N + 1; ++j)
			{
				if(dp[j][i] == maxVal)
				{
					index = i;
					break;
				}
			}
		}
		cout << "Case " << count << ":" << endl;
		cout << maxVal << endl;
		cout << index << endl;
	}
}

int main()
{
	solve();
	return(0);
}