#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int W, N, count = 0;

	while(cin >> W, W) {
		count++;
		cin >> N;
		vvi data(N, vi(2));
		char c;
		REP(i, N) {
			cin >> data[i][0] >> c >> data[i][1];
		}

		vvi dp(N+1, vi(W+1));
		REP(j, W+1) {
			dp[0][j] = 0;
		}

		REP(i, N) {
			FOR(j, 0, W) {
				if(j < data[i][1]) {
					dp[i+1][j] = dp[i][j];
				} else {
					dp[i+1][j] = max(dp[i][j], dp[i][j-data[i][1]]+data[i][0]);
				}
			}
		}

		int ans_w = 0, ans_j;
		FOR(j, 0, W) {
			if(ans_w < dp[N][j]) {
				ans_w = dp[N][j];
				ans_j = j;
			}
		}

		cout << "Case " << count << ":" << endl;
		cout << ans_w << endl;
		cout << ans_j << endl;
	}

	return 0;
}