#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int dp[1005][1005];

int main()
{
//	cut here before submit 
//	freopen ("testcase.A_Thief", "r", stdin );
	int cnt = 0;
	while (1 ){
		int W;
		scanf ("%d", &W );
		if (W == 0 ) break;
		int N;
		scanf ("%d", &N );
		vector<int> v (N, 0 ), w (N, 0 );
		rep (i, N ){
			string str;
			cin >> str;
			while (str.find(',') != string::npos ){
				str = str.replace(str.find(','), 1, " ");
			} // end while
			stringstream ss (str );
			ss >> v[i] >> w[i]; 
		} // end rep
/*
		rep (i, N ){
			cerr << "v: " << v[i] << " w: " << w[i] << endl;
		} // end rep
*/
		memset (dp, 0, sizeof (dp ) );
		dp[0][w[0]] = v[0];
		for (int i = 1; i < N; i++ ){
			dp[i][w[i]] = v[i];
			for (int j = 0; j <= W; j++ ){
				if (dp[i-1][j] != 0 ){
					dp[i][j] = max (dp[i][j], dp[i-1][j] );
					if (j + w[i] <= W ){
						dp[i][j + w[i]] = max (dp[i][j + w[i]], dp[i-1][j] + v[i] );
					} // end if
				} // end if
			} // end for
		} // end for
/*
		rep (i, N ){
			rep (j, W ){
				cerr << setw(3) << dp[i][j];
			} // end rep
			cerr << endl;
		} // end rep
*/
		int res_v = 0, res_w = 0;
		for (int j = 0; j <= W; j++ ){
			if (dp[N-1][j] > res_v ){
				res_v = dp[N-1][j];
				res_w = j;
			} // end if
		} // end for
		printf ("Case %d:\n", ++cnt );
		printf ("%d\n", res_v );
		printf ("%d\n", res_w );
	} // end loop

//	printf ("%d\n", res );
		
	return 0;
}