#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 1001
typedef pair<int,int> P;

int dp[MAX][MAX],t,w,m;

void judge(vector<P> v){
    
    for(int i = 1 ; i <= m ; i++){
        for(int j = 1 ; j <= w ; j++){
            int k = j - v[i].first;
            if(k < 0){
                dp[i][j] = dp[i-1][j];
            }
            else{
                dp[i][j] = max(dp[i-1][j],dp[i-1][k]+v[i].second);
            }
        }
    }
    int ans_W=0,ans_C;
    for(int i = 1 ; i <= w ; i++){
        ans_W = max(ans_W,dp[m][i]);
    }
    for(int i = w ; i >= 1 ; i--){
        if(ans_W==dp[m][i]) ans_C = i;
    }
    
    cout << ans_W << endl;
    cout << ans_C << endl;
}

void print(){
    cout << "Case " << t++ << ":" << endl;
}

int main(){
    int val,wei;
    
    t = 1;
    while(cin >> w,w){
        cin >> m;
        vector<P> v;
        
        fill(dp[0],dp[MAX-1],0);
        v.push_back(P(0,0));
        
        print();
        for(int i = 0 ; i < m ; i++){
            scanf("%d,%d" ,&val ,&wei);
            v.push_back(P(wei,val));
        }
        sort(v.begin(),v.end());
        judge(v);
    }
    
    return 0;
}