#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define NMAX 1000
#define WMAX 1000
#define DIAGONAL 1
#define TOP 0

using namespace std;

struct Item{
	int value,weight;
};

int N,W;
Item items[NMAX+1];
int C[NMAX+1][WMAX+1],G[NMAX+1][WMAX+1];
int total_weight = 0;

void compute(int &maxValue,vector<int>&selection){
	for(int w=0;w <= W;w++){
		C[0][w] = 0;
		G[0][w] = DIAGONAL;
	}

	for(int i=1;i<=N;i++)C[i][0] = 0;

	for(int i=1;i<=N;i++){
		for(int w=1;w<=W;w++){
			C[i][w] = C[i-1][w];
			G[i][w] = TOP;
			if(items[i].weight > w)continue;
			if(items[i].value+C[i-1][w-items[i].weight] > C[i-1][w]){
				C[i][w] = items[i].value + C[i-1][w - items[i].weight];
				G[i][w] = DIAGONAL;
			}
		}
	}
	maxValue = C[N][W];
	selection.clear();
	for(int i=N,w=W;i>=1;i--){
		if(G[i][w] == DIAGONAL){
			selection.push_back(i);
			w -= items[i].weight;
			total_weight += items[i].weight;
		}
	}
	reverse(selection.begin(),selection.end());

}

int main(){
	int maxValue;
	int case_number = 1;
	vector<int>selection;

	scanf("%d",&W);
	while(W != 0){
		memset(items,0,sizeof(items));
		total_weight=0;
		scanf("%d",&N);
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&items[i].value,&items[i].weight);
		}

		compute(maxValue,selection);

		cout << "Case " << case_number << ":" << endl;
		cout << maxValue << endl;
		cout << total_weight - 1 << endl;
		scanf("%d",&W);
	}

	return 0;
}