#include <bits/stdc++.h>

int a, b, n, w;
std::string s;
int c = 0;

int main() {
    while (true) {
        std::cin >> w;
        if (w == 0) {
            return 0;
        }
        std::cin >> n;
        std::vector<std::pair<int, int>> things;
        for (int i = 0; i < n; i++) {
            std::cin >> s;
            std::sscanf(s.c_str(), "%d,%d", &a, &b);
            things.push_back({a, b});
        }
        std::vector<std::vector<int>> dp(n+1, std::vector<int>(w+1, 0));
        for (int j = 0; j < n; j++) {
            for (int p = 1; p <= w; p++) {
                if (things[j].second <= p) {
                   dp[j+1][p] =  std::max(dp[j][p], dp[j][p-things[j].second] + things[j].first);
                }
                else {
                    dp[j+1][p] = dp[j][p];
                }
            }
        }
        int weight = 0;
        int thief_value = 0;
        for (int i = 0; i <= w; i++) {
            if (dp[n][i] > thief_value) {
                weight = i;
                thief_value = dp[n][i];
            }
        }
        c++;
        std::cout << "Case "<< c <<":" << std::endl << dp[n][w] << std::endl << weight << std::endl;
    }
}
