#include<iostream>
using namespace std;
int main(){
long ww,n,v,*w,**p,i,j,k=1,sw;
char c;
while(cin>>ww){
if(ww==0)break;
cin>>n;
w=new long[n+1];
p=new long*[n+1];
for(i=0;i<=n;i++){p[i]=new long[ww+1];p[i][0]=0;}
for(j=0;j<=ww;j++)p[0][j]=0;
sw=0;
for(i=1;i<=n;i++){
cin>>v>>c>>w[i];
for(j=1;j<=ww;j++){
if(w[i]>j)p[i][j]=p[i-1][j];
else if(p[i-1][j-w[i]]+v>p[i-1][j])p[i][j]=p[i-1][j-w[i]]+v;
else p[i][j]=p[i-1][j];
}
}
for(j=ww;j>=1;j--)if(p[n][j]!=p[n][j-1])break;
for(i=n;i>=1;i--){
if(p[i][j]!=p[i-1][j]){sw+=w[i];j-=w[i];}
}
cout<<"Case "<<k<<":"<<endl;
cout<<p[n][ww]<<endl;
cout<<sw<<endl;
k++;
}
return 0;
}