#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int W;
    int c = 0;
    while(++c, cin >> W, W) {
        int N;
        cin >> N;
        vector<vector<int>> dp(N+1, vector<int>(W+1, -1));
        dp[0][0] = 0;
        for(int i=0; i<N; ++i) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for(int j=0; j<=W; ++j) {
                dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
                if(j+w <= W && dp[i][j] != -1) {
                    dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j] + v);
                }
            }
        }
        int w = 0;
        for(int i=0; i<=W; ++i) {
            if(dp[N][w] < dp[N][i]) {
                w = i;
            }
        }
        cout << "Case " << c << ':' << endl;
        cout << dp[N][w] << endl;
        cout << w << endl;
    }
}
