#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll dp[2][1005];

int main() {
	int W, N, wei[1005], cost[1005],t,s,r=0;
	while (cin >> W >> N, W) {
		r++;
		t = N  % 2;
		fill(dp[0], dp[0] + N, 0);
		fill(dp[1], dp[1] + N, 0);
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &wei[i], &cost[i]);
		}
		for (int i = 0; i < N; i++) {
			for (int w = 0; w <= W; w++) {

				if (w - wei[i] >= 0) dp[(i + 1) % 2][w] = max(dp[i][w], dp[i][w - wei[i]] + cost[i]);
				
			}
		}
		for (int i = 0; dp[t][W - i]==dp[t][W]; i++) {
			s = i;
		}
		cout << "Case " << r << ":" << endl;
		cout << dp[t][W] << endl;
		cout << W-s << endl;
	}
}