#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e5 + 5);
const double EPS = 1e-11;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int W,n,v[1005],w[1005],dp[1005][1005];

int main() {
    int cnt=1;
    string tmp;
    while(cin >> W,W){
        cin >> n;
        REP(i,n){
            scanf("%d,%d",&v[i],&w[i]);
        }
        REP(i,n+1)fill(dp[i],dp[i]+1005,0);
        REP(i,n){
            REP(j,W+1){
                if(j>=w[i]) dp[i+1][j] = max(dp[i+1][j],dp[i][j-w[i]]+v[i]);
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
        int wmin = W;
        REPR(i,W){
            if(dp[n][i]!=dp[n][W])break;
            wmin = i;
        }
        cout << "Case " << cnt << ":" << endl;
        cout << dp[n][W] << endl;
        cout << wmin << endl;
        cnt++;
    }
    return 0;
}

