#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <cstdio>
#include <cctype>
#include <queue>
#include <complex>
#include <climits>
#include <cstring>
#include <cstdlib>

typedef long long ll;

using namespace std;

int dp[1010][1010];

int main(void){
	int W, N;
	int w[10010], v[10010];
	int c = 0;

	while(cin >> W){
		c++;
		if(!W) break;
		cin >> N;
		for(int i=0; i<N; ++i){
			scanf("%d,%d", &v[i], &w[i]);
		}

		for(int i=0; i<=N; ++i){
			for(int j=0; j<=W; ++j){
				dp[i][j] = 0;
			}
		}

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

		int weight;
		for(int j=W; j>=0; --j){
			if(dp[N][j] == dp[N][W]) weight = j;
			else break;
		}

		cout << "Case " << c << ":" << endl << dp[N][W] << endl << weight << endl;

	}


	return 0;
}