#include <iostream>
#include <vector>

using namespace std;

int main() {
    int W, N, v, w;
    char c;
    int case_ = 0;

    while (cin >> W) {
        if (W == 0) break;
        cin >> N;
        case_++;

        int dp[N + 1][W + 1];
        fill(dp[0], dp[N + 1], 0);
        dp[0][0] = 0;

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

        int max_v = dp[N][W];
        int min_w;

        for (int i = W; i >= 0; i--) {
            if (dp[N][i] == max_v) min_w = i;
            else break;
        }

        cout << "Case " << case_ << ":" << endl;
        cout << max_v << endl;
        cout << min_w << endl;
    }
}
