#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi){
    P p;
    if(chg[idx][wi]) return dp[idx][wi];
    if(idx == n){
        p = P(0,wi);
    }
    else{
        if(wi - dat[idx].sc >= 0){
            p = rec(idx+1,wi-dat[idx].sc);
            p.first += dat[idx].fr;
            p = max(p,rec(idx+1,wi));
        }
        else p = rec(idx+1,wi);
    }
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
        dat.clear();
    }
}