#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int w[1000],v[1000];
int W,N;
int memo[1001][1001];
int func(int i,j){
    if(memo[i][j]>=0)return memo[i][j];
    
    int res;
    if(i==N)res=0;
    else if(j-w[i]<0)res=func(i+1,j);
    else res=max(func(i+1,j),func(i+1,j-w[i])+v[i]);
    
    return memo[i][j]=res;
}
int main(){
    int cnt=0;
    while(cin>>W,W){
        cin>>N;
        cnt++;
        for(int i=0;i<N;i++)(cin>>v[i]).ignore()>>w[i];
        fill(memo[0],memo[0]+1001*1001,-1);
        memo[0][0]=0;
        func(0,W);
        
        for(int i=W;i>=0;i--){
            if(memo[0][i]==-1)continue;
            if(memo[0][i]>memo[0][i-1]){
                cout<<"Case "<<cnt<<":"<<endl;
                cout<<memo[0][i]<<endl<<i<<endl;
                break;
            }
        }
    }
    return 0;
}