#include <iostream>
#include <complex>

typedef std::complex<int> P;
using namespace std;

#define SAFE_DELETE(p) {\
if(p != NULL){\
	delete [] p;\
}\
}

int w,n;
int* item;
int* price;

P dfs(int index, P now)
{
	if(now.real() > w)
	{
		return P(0, -100);
	}
	if(index >= n)
		return now;
	P next = P(now.real()+item[index], now.imag()+ price[index]);
	P a = dfs(index+1, next);
	P b = dfs(index+1, now);
	if(a.imag() > b.imag())
	{
		return a;
	}
	else if(a.imag() < b.imag())
	{
		return b;
	}
	else if(a.real() > b.real())
	{
		return b;
	}
}

int main(int argc, char *argv[]) {
	item = NULL;
	price = NULL;
	int i=1;
	for(;;)
	{
		cin >> w >> n;		
		if(n <= 0)
			return 0;
		SAFE_DELETE(item);
		SAFE_DELETE(price);
		item = new int[n];
		price = new int[n];
		
		char delim = ',';
		for(int i=0; i<n; i++)
		{
			cin >> price[i] >> delim >> item[i];
		}
		P ans = dfs(0, P(0,0));
		cout << "Case "<< i << ":" << endl;
		cout << ans.imag() << endl;
		cout << ans.real() << endl;

		i++;
	}

}