#include <iostream>
#include <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

Int mod_pow(Int x, Int n) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

Int v[1005];
Int w[1005];
Int dp[1005][1005];
vector<Int> ans;
vector<Int> ans2;

void solve(Int W){
    Int N; cin >> N;
    memset(v, 0, sizeof(v));
    memset(w, 0, sizeof(w));
    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < N; i++){
        char c;
        cin >> v[i] >> c >> w[i];
    }
    for (int i = 0; i < N; i++){
        for (int j = 0; j <= W; j++){
            if (j+w[i] <= W) dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j] + v[i]);
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        }
    }
    ans.push_back(dp[N][W]);
    for (int i = W; i >= 0; i--){
        if (dp[N][i] != dp[N][W]){
            ans2.push_back(i+1);
            break;
        }
    }

}

int main(){
    cin.tie(0);

    while (1){
        Int W; cin >> W;
        if (!W) break;
        solve(W);
    }
    for (int i = 0; i < ans.size(); i++){
        cout << "Case " << i+1 << ":" << endl;
        cout << ans[i] << endl;
        cout << ans2[i] << endl;
    }
}

