#include <string>
#include <iostream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <vector>
 
using namespace std;
 

int main(){
	int n = 1;

	while(true){
		int W, N;
		cin >> W;
		if(W == 0){
			break;
		}

		cin >> N;
		int w[1001];
		int v[1001];
		for(int i = 0; i < N; i++){
			scanf("%d,%d", &v[i], &w[i]);
		}
		vector<int> best(W+1);
		best.assign(best.size(), 0);
		for(int i = 0; i < N; i++){
			for(int j = W; j >= 0; j--){
				if(j == 0 || best[j] != 0){
					if(j+w[i] <= W){
						best[j+w[i]] = best[j]+v[i];
//						cout << "best[" << j+w[i] << "]:" << best[j+w[i]] << endl;
					}
				}
			}
		}
		int m = W;
		for(int i = W; i >= 0; i--){
			if(best[i] >= best[m]){
				m = i;
			}
		}
		cout << "Case " << n++ << ":" << endl;
		cout << best[m] << endl;
		cout << m << endl;
	}
  
    return 0;
}