#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int dp[1001][1001];
int N, W;
int v[1001], w[1001];

int solve(int i, int j) {
	if (dp[i][j] != -1)return dp[i][j];

	int res;
	if (i == N)res = 0;
	else if (j < w[i])res = solve(i + 1, j);
	else {
		res = max(
			solve(i + 1, j),
			solve(i + 1, j - w[i]) + v[i]
		);
	}
	return dp[i][j] = res;
}

int main() {
	int now = 1;
	while (1) {
		cin >> W;
		if (W == 0)break;
		cin >> N;
		for (int i = 0; i < N; i++) {
			scanf_s("%d,%d", v + i, w + i);
		}

		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = -1;
			}
		}
		solve(0, W);
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				cout << i << ":" << j<<"=" << dp[i][j] << endl;
			}
		}
		int value = -1; int weight;
		for (int i = 0; i <= W;i++) {
			if (value < dp[0][i]) {
				value = dp[0][i];
				weight = i;
			}
		}
		cout << "Case " << now++ << ":" << endl << value << endl << weight-1 << endl;
	}
}
