#include <math.h>
#include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main(){

	int n,w;

	for ( int num = 1;; num++ ) {

		cin >> w;
		if ( w == 0 ) break;
		cin >> n;

		int input1,input2;
		char input3;
		vector<int> value;
		vector<int> weight;
		for ( int i = 0; i < n; i++ ) {
			cin >> input1 >> input3 >> input2;
			value.push_back(input1);
			weight.push_back(input2);
		}

		vector<int> check;
		check.push_back(0);
		for ( int i = 0; i < w-1; i++ ) {
			check.push_back(-1);
		}
		int value_sum = 0;
		int weight_sum = w+1;

		for ( int i = 0; i < n; i++ ) {
			for ( int j = w - weight[i] - 1; j >= 0; j-- ) {
				if ( check[j] != -1 ) {
					check[ j+weight[i] ] = max( check[ j+weight[i] ], check[j] + value[i] );
					if ( check[ j+weight[i] ] > value_sum ) {
						value_sum = check[ j+weight[i] ];
						weight_sum = j+weight[i];
					}else if ( check[ j+weight[i] ] == value_sum ) {
						weight_sum = min( weight_sum, j+weight[i] );
					}
				}
			}
		}
		cout << "Case " << num << ":" << endl << value_sum << endl << weight_sum << endl;
	}
	return 0;
}