#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int W,N;
int dp[1001][1001]; // 宝の数×重さ
struct Obj {
	int price;
	int weight;
} obj[1001];

int main(void){
	int cnt = 1;
	while (cin >> W, W) {
		// input
		cin >> N;
		char buf;
		for (int i = 0; i < N; i++) {
			cin >> obj[i].price >> buf >> obj[i].weight;
		}
		
		// init
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = 0;
			}
		}
		dp[0][obj[0].weight] = obj[0].price;

		// calc
		int wei;
		for (int i = 0; i < N - 1; i++) {
			for (int j = 0; j <= W; j++) {
				if (j != 0 && dp[i][j] == 0) {
					continue;
				}
				// 下
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				// 右下
				if (wei = j + obj[i + 1].weight, wei <= W) {
					dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price);
				}
			}
		}
		
		struct Obj res = {0, 0};
		for (int i = 0; i <= W; i++) {
			for(int j = 0; j < N; j++) {
				if (res.price < dp[j][i]) {
					res.price = dp[j][i];
					res.weight = i;
				}
			}
		}

		// output
		printf("Case %d:\n%d\n%d\n", cnt, res.price, res.weight);
	}

	return 0;
}