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

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

#define all(c) c.begin(),c.end()
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

typedef long long ll;
typedef complex<double> point;
typedef vector<int> vi;

template<class T>
inline int len(const T& t){
    return t.size();
}

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& val){
    os << "[ ";
    for(typename vector<T>::const_iterator it=val.begin();
        it != val.end();++it){
        os << *it << " ";
    }
    os << "]";
    return os;
}

struct Treasure{
    int value,weight;
    Treasure():
        value(0),weight(0) {};
    Treasure(int _value,int _weight)
        : value(_value),weight(_weight) {}
};

int main(){
    for(int loop_count=1;true;loop_count++){
        int W,N;
        cin >> W;
        if(W == 0) break;
        cin >> N;
        vector<Treasure> v(N);
        rep(i,N){
            char c;
            cin >> v[i].value >> c >> v[i].weight;
        }
        vi value(W+1);
        rep(n,N){
            vi tmp(W+1);
            rep(i,W+1){
                tmp[i] = max(tmp[i],value[i]);
                int in = i+v[n].weight;
                if(in <= W){
                    tmp[in] = max(tmp[in],value[i]+v[n].value);
                }
            }
            value.swap(tmp);
        }
        int retw=0;
        int retv=0;
        rep(i,len(value)){
            if(value[i] > retv){
                retv = value[i];
                retw = i;
            }
        }
        cout << "Case " << loop_count << ":" << endl;
        cout << retv << endl << retw << endl;
    }
    return 0;
}