#include<iostream>
using namespace std;
int W,N,ansap,answp,a,b,c=0,nw;
int dp[1001][1001];
int ap[1001];
int wp[1001];
char mj [5];


int main(){
  char ch;
  while(true){
    c++;
    cin>>W;
    if(W==0)break;
    cin>>N;
    for(int i=1;i<=N;i++){
      cin>>ap[i];
      cin >> ch;
      cin>>wp[i];
    }
    
    for(int i=0;i<1001;i++){
      dp[0][i]=0;
      dp[i][0]=0;
    }
    
    for(int i=1;i<=N;i++){
      for(int j=1;j<=W;j++){
	nw=j-wp[i];
	if(nw>=0){
	  if(dp[i-1][j]<dp[i-1][nw]+ap[i]){
	    dp[i][j]=dp[i-1][nw]+ap[i];
	  }else{
	    dp[i][j]=dp[i-1][j];
	  }
	}else{
	  dp[i][j]=dp[i-1][j];
	}
      }
    }
    ansap=0;
    for(int i=1;i<=W;i++){
      if(ansap<dp[N][i]){
	ansap=dp[N][i];
	answp=i;
      }
    }
    


    cout<<"Case "<<c<<":"<<endl;
    cout<<ansap<<endl;
    cout<<answp<<endl;
  }
  return 0;
}