#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

#define REP(i, n) for ( int i = 0; i < n; i++ )

using namespace std;

/**
wがあふれないようにする
価値が最大

dp[重さ] = 最大価値
*/

int dp[1000] = {-1};

int main() {
    int w, cases = 1;
    
    while(cin >> w, w) {
        int n, a[1000][2];
        cin >> n;

        for ( int i = 0; i < n; i ++ ) {
            scanf("%d,%d", &a[i][1], &a[i][0]);
        }

        for ( int i = 0; i < n; i++ ) {
            for ( int j = 0; j < n; j++ ) {
                int ww = a[i][0] + a[j][0],
                    p = a[i][1] + a[j][1];
                if ( ww > w ) continue;
                if ( p > dp[ww] ) dp[ww] = p;
            }
        }
        int max_i = 0;
        for ( int i = 0; i < w; i++ ) {
            if ( dp[i] > dp[max_i] ) max_i = i;
        }
        printf("Case %d:\n%d\n%d\n", cases++, max_i, dp[max_i]);
    }
    
    return 0;
}