#include <iostream>
#include <map>
#include <stdio.h>
int main(){
  using std::cin;
  using std::cout;
  using std::endl;
  using std::map;
  int weight,n,w,v,s,c=1;
  map<int,int> m;
  while(cin>>weight){
    if(!weight)return 0;
    cin>>n;
    map<int,int>::iterator it;
    for(;n;n--){
      scanf("%d,%d",&v,&w);
      if(m.empty()){m[w]=v;continue;}
      it = m.begin();
      while(it!=m.end()){
	s=(*it).second+v;
	  if((*it).first+w<weight){
	    if(m[(*it).first+w]<s)m[(*it).first+w]=(*it).second+v;
	  }
	it++;
      }
      m[w]=v;
    }
    it=m.end();
    it--;
    cout<<"Case "<<c++<<":"<<endl<<(*it).second<<endl<<(*it).first<<endl;
    m.clear();
  }
}