#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1000;
const int MAX_W = 1000;
int dp[MAX_N + 1][MAX_W + 1];

int main() {
    int id = 1;
    int W, N;
    while (scanf("%d", &W), W) {
        cin >> N;

        int w[MAX_N + 1], v[MAX_N + 1];
        for (int i = 0; i < N; i++) {
            scanf("%d,%d", &v[i], &w[i]);
        }

        for (int i = 0; i <= W; i++) {
            dp[N][i] = 0;
        }

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

        int max_value = 0, max_weight;
        for (int i = 0; i <= W; i++) {
            if (dp[0][i] <= max_value) continue;
            max_value = dp[0][i];
            max_weight = i;
        }

        printf("Case %d:\n", id++);
        printf("%d\n%d\n", max_value, max_weight);
    }

    return 0;
}
