#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(){
	int w, casecount = 0;
	while(cin >> w && w != 0){
		casecount++;
		int n;
		cin >> n;
		
		vector<vector<int> > table(n + 1, vector<int>(w + 1, 0));
		vector<pair<int, int> > trea;
		
		for(int i = 0; i < n; i++){
			pair<int, int> tmp;
			scanf("%d,%d", &tmp.first, &tmp.second);
			trea.push_back(tmp);
		}
		
		for(int i = 1; i < n + 1; i++){
			for(int j = 1; j < w + 1; j++){
				if(j < trea[i - 1].second){
					table[i][j] = table[i - 1][j] > table[i][j] ? table[i - 1][j] : table[i][j];
				}
				else{
					table[i][j] = table[i - 1][j] > trea[i - 1].first + table[i - 1][j - trea[i - 1].second] ? table[i - 1][j] : trea[i - 1].first + table[i - 1][j - trea[i - 1].second];
				}
			}
		}

		int max = -1, cost = -1;
		
		for(int j = 0; j < w + 1; j++){
			if(max < table[n][j]){
				max = table[n][j];
				cost = j;
			}
		}

		printf("Case %d:\n%d\n%d\n", casecount, max, cost);		
	}
	return 0;
}