#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

int main() {
    int count = 1;
    while (true) {
        int limit;
        cin >> limit;
        if (limit == 0)break;
        int n;
        cin >> n;

        vector<int> dp(limit + 1, -1145141919);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            char s[15];
            cin >> s;
            int v = stoi(strtok(s, ","));
            int w = stoi(strtok(NULL, ","));
            for (int j = limit; j >= 0; j--) {
                if (j + w > limit)continue;
                dp[j + w] = max(dp[j + w], dp[j] + v);
            }
        }

        int maxvalue = 0;
        int minweight = 0;
        for (int i = 0; i <= limit; i++) {
            if (maxvalue < dp[i]) {
                maxvalue = dp[i];
                minweight = i;
            }
        }

        cout << "Case " << count << ":" << endl;
        cout << maxvalue << endl;
        cout << minweight << endl;
        count++;
    }
    return 0;
}
