#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                             it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1,0,1,0};
const int dx[] = {0,1,0,-1};

const double EPS = 1e-9;
const int days[]     = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main(){

    for(int loop = 1;;loop++){
        int N,W;
        cin >> W;
        if(W == 0) break;
        cin >> N;
        // value,weight
        vector<pair<int,int> > T(N);
        char c;
        rep(i,N) cin >> T[i].first >> c >> T[i].second;
        vector<vector<int> > DP(N,vector<int>(W+1));

        for(int i=0;i<=W;i++){
            if(T[0].second <= i){
                DP[0][i] = T[0].first;
            }
        }

        for(int i=1;i<N;i++){
            for(int j=0;j<=W;j++){
                if(0 <= j-T[i].second){
                    DP[i][j] = max(DP[i-1][j],DP[i-1][j-T[i].second]+T[i].first);
                }else{
                    DP[i][j] = DP[i-1][j];
                }
            }
        }

        int maxi = -1;
        int maxk = -1;
        for(int i=0;i<=W;i++){
            if(maxi < DP[N-1][i]){
                maxi = DP[N-1][i];
                maxk = i;
            }
        }
        cout << "Case " << loop << ":" << endl;
        cout << maxi << endl << maxk << endl;
    }
    return 0;
}