#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int N_MAX = 1000;
const int W_MAX = 1000;
int c[N_MAX+1][W_MAX+1];

int main() {
    int t = 1;
    int w;
    while (cin >> w, w) {
        int n;
        cin >> n;
        vector<int> vs(n+1), ws(n+1);
        for (int i = 1; i <= n; i++) {
            scanf("%d,%d", &vs[i], &ws[i]);
        }
        for (int i = 0; i <= n; i++) {
            fill_n(c[i], w+1, 0);
        }
        int v_max = -1, w_min = 1 << 29;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= w; j++) {
                if (j - ws[i] >= 0) {
                    c[i][j] = max(c[i-1][j], c[i-1][j - ws[i]] + vs[i]);
                    if (v_max < c[i][j]) {
                        v_max = c[i][j];
                        w_min = j;
                    } else if (v_max == c[i][j]) {
                        w_min = min(w_min, j);
                    }

                } else {
                    c[i][j] = c[i-1][j];
                }
            }
        }
        printf("Case %d:\n%d\n%d\n", t++, v_max, w_min);
    }
}