#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

int dp[1001][1001];

int main()
{
	int c = 1;

	while(1)
	{

		memset(dp, 0, sizeof(dp));

		int w, n;
		cin>>w>>n;
		if(w == 0)break;
		int treasure[1000];
		int price[1000];

		for(int i = 0; i < n; i++)
			scanf("%d,%d", &price[i], &treasure[i]);

		for(int i = 0; i < n; i++){
			for(int j = 0; j <= w; j++){
				if(j < treasure[i])
					dp[i + 1][j] = dp[i][j];
				else
					dp[i + 1][j] = max(dp[i][j], dp[i][j - treasure[i]] + price[i]);
			}
		}
	
		int ansWeight;
		for(ansWeight = w - 1; w >= 0; ansWeight--)
			if(dp[n][w] > dp[n][ansWeight])break;
			
		cout << "Case" << c++ <<":"<<endl;
		cout << dp[n][w] << endl;
		cout << ansWeight + 1 << endl;
	}

	return 0;
}