#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(){
	freopen("test.txt", "r", stdin);
	int casenum = 0;
	while(1){
		int W, n;
		cin >> W >> n;
		if(!W) break;

		vector<int> w(n+1);
		vector<int> v(n+1);
		FOR(i, 1, n+1){
			scanf("%d,%d", &v[i], &w[i]);
		}

		vector<vector<int> > table(n+1, vector<int>(W+1));

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


		int idx = 0;
		rep(i, W+1){
			if(table[n][idx] < table[n][i]){
				idx = i;
			}
		}

		cout << "Case " << ++casenum << ':' << endl;
		cout << table[n][idx] << endl;
		cout << idx << endl;
	}

	return 0;
}