#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    cin>>n;
    vector<int>in(m,0);
    rep(i,n){
      int a,b;
      char d;
      cin>>a>>d>>b;
      for(int i=m;i>0;i--){
	if(i-b<0)continue;
	if(i!=b&&!in[i-b])continue;
	in[i]=max(in[i-b]+a,in[i]);
      }
      //rep(i,m+1)cout<<in[i]<<" "<<i<<endl;
      //cout<<" "<<endl;
    }
    int ans1=0,ans2=0;
    for(int i=m;i>0;i--){
      if(ans1<=in[i]){ans1=in[i];ans2=i;}
    }
    cout<<"Case "<<co<<":"<<endl;
    cout<<ans1<<endl<<ans2<<endl;
  }
}