#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 1000000000;
const double EPS = 1e-10;

int main(){
	int casenum = 0;
	while(1){
		int w, n;
		cin >> w >> n;
		if(!w) break;

		n++;
		vector<P> item(n); // value-weight
		item[0] = MP(0,0);
		FOR(i, 1, n){
			scanf("%d,%d", &item[i].first, &item[i].second);
		}
		vector<vector<int> > v(n, vector<int>(w+1, 0));

		FOR(i, 1, n){
			for(int j = 0; j+item[i].second <= w; j++){
				v[i][j+item[i].second] = max( v[i-1][j+item[i].second], v[i-1][j]+item[i].first );
			}
		}

		int idx = 0;
		rep(i, w+1){
			if(v[n-1][idx] < v[n-1][i]){
				idx = i;
			}
		}

		cout << "Case" << ++casenum << ':' << endl;
		cout << v[n-1][idx] << endl;
		cout << idx << endl;
	}

	return 0;
}