#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <stack>
#define rep(i,k,n) for(int i=k;i<n;i++)
#define pb push_back

typedef long long ll;
using namespace std;
void komma(int& nedan,int& omosa){
  char inputl[100];
  cin>>inputl;
  rep(i,0,100){
    if(inputl[i]==',') {
      inputl[i]=0;
      nedan=atoi(inputl);
      omosa=atoi(inputl+i+1);
      break;
    }
  }
  //  cout <<nedan<<endl<<omosa<<endl;
}
int main(){

  int W,N,nedan,omosa,maxne,maxomo,count,tmp;
 
  vector<vector<int> >hukuro(1010);
  rep(i,0,1010)
    hukuro[i]=vector<int>(1010,0);
  count=0;
  cin>>W>>N;
  //cout<<"??\n";
  while(W){
    maxne=maxomo=0;
    rep(i,0,1000)
      hukuro[0][i]=0;
    komma(nedan,omosa);
    //cout<<"otto?\n";  
    hukuro[0][omosa]=nedan;
    rep(i,1,N){
    komma(nedan,omosa);
    rep(j,0,W+1){
      if(j>=omosa)
	hukuro[i][j]=max(hukuro[i-1][j],hukuro[i-1][j-omosa]+nedan);
      else hukuro[i][j]=hukuro[i-1][j];
      // cout<<i<<" "<<j<<endl<<hukuro[i][j]<<endl;
    }
    }
    for(int i=W;i>=0;i--)
      if(maxne<=hukuro[N-1][i]){
	maxne=hukuro[N-1][i];
	maxomo=i;
      }
    cout<<"Case "<<++count<<":\n"<<maxne<<endl<<maxomo<<endl;
    cin>>W>>N;
    //    cout<<"???\n";
    }

  return 0;
}