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

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

long long int dp[1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[i] = 0;
        }

        dp[0] = 0;
        for(int i=0;i<n;i++) {
            for (int j = w; j != 0; j--) {
                if(j > t[i].second) {
                    dp[j] = max(dp[j], dp[j - t[i].second] + t[i].first);
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[i]){
                maxvalue = dp[i];
                thenweight = i;
            }
        }

        printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
    }
    return 0;
}