#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define INF (1<<29)
#define mp make_pair
#define pb push_back

typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;

int main(){
	int W;
	for( int n = 1; cin >> W; n++ ){
		if( W == 0 ) break;
		int N;
		cin >> N;
		vi v(N), w(N);
		for( int i = 0; i < N; i++ ){
			scanf("%d,%d", &v[i], &w[i]);
		}
		// DP
		vector<vector<pii> > dp(N+1, vector<pii>(W+1,mp(0,0)));
		for( int i = 1; i <= N; i++ ){
			for( int j = 1; j <= W; j++ ){
				pii upperLeft = dp[i-1][j-w[i-1]], upper = dp[i-1][j];
				if( j-w[i-1] < 0 ){
					dp[i][j] = upper;
				}else{
					if( upper.first == upperLeft.first+v[i-1] ){
						dp[i][j] = mp(upper.first, min(upper.second, upperLeft.second+w[i-1]));
					}else if( upper.first < upperLeft.first+v[i-1] ){
						dp[i][j] = mp(upperLeft.first+v[i-1], upperLeft.second+w[i-1]);
					}else{
						dp[i][j] = upper;
					}
				}
			}
		}
		cout << "Case " << n << ":" << endl;
		cout << dp[N][W].first << endl;
		cout << dp[N][W].second << endl;
	}
}