#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b){
    if(a<b) swap(a, b);
    if(b==0) return a;
    return gcd(b, a%b);
}

int W, N, cnt=1;
const int MAX_W = 1000;
const int MAX_N = 1000;
int dp[MAX_N+1][MAX_W+1];
int w[MAX_N]; int v[MAX_N];
int main() {
    while(cin >> W, W){
        cin >> N;
        for(int i=0; i<N; ++i){
            scanf("%d,%d", &v[i], &w[i]);
        }
        int sum_w=0;
        dp[0][0] = 0;
        for(int i=N-1; i>=0; --i){
            for(int j=0; j<=W; ++j){
               if(j<w[i]){
                   dp[i][j] = dp[i][j];
               }else{
                   dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
               }
            }
        }
        for(int i=N-1; i>=0; --i){
            for(int j=0; j<=W; ++j){
                if(dp[i][j]==dp[0][W]){
                    sum_w = j;
                    break;
                }
            }
            if(sum_w) break;
        }
        cout << "Case " << cnt << ":" << endl;
        cout << dp[0][W] << endl;
        cout << sum_w << endl;
        cnt++;
    }
    return 0;
}

