#include <iostream>
#include <map>

using namespace std;

typedef pair<unsigned long long int, int> P;

P plus_(P a, P b) {
    return P(a.first+b.first, a.second+b.second);
}

int main() {
    int w, n;
    int value[1000], weight[1000];
    P dp[2][1000];
    for (int case_num=1; true; case_num++) {
        cin >> w;
        if (w == 0) {
            return 0;
        }
        cin >> n;
        for (int i=0; i<n; i++) {
            cin >> value[i];
            cin.ignore(1);
            cin >> weight[i];
        }
        for (int i=0; i<=w; i++) {
            dp[0][i] = P(0, 0);
        }
        for (int i=1; i<=n; i++) {
            for (int j=0; j<=w; j++) {
                if (j < weight[i-1]) {
                    dp[i%2][j] = dp[(i-1)%2][j];
                }
                else {
                    P a = plus_(dp[(i-1)%2][j-weight[i-1]], P(value[i-1], -weight[i-1]));
                    P b = dp[(i-1)%2][j];
                    dp[i%2][j] = max(a, b);
                }
            }
        }
        cout << "Case " << case_num << ":" << endl;
        cout << dp[n%2][w].first << endl;
        cout << -dp[n%2][w].second << endl;
    }
}