#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int m;
int w,n;
int dp;
char t;
vector<pair <int,int> > pa;
pair<int,int> tpa;
pair <pair <int,int> , int> MAX;

int foo(int w,int v,int c,int d){
	if(c==pa.size()){
		tpa=MAX.first;
		if(w<=m&&MAX.second>=d&&tpa.second==v){
			tpa.first=w;
			tpa.second=v;
			MAX.first=tpa;
			MAX.second=d;
		}
		else if(w<=m&&tpa.second<v){
			tpa.first=w;
			tpa.second=v;
			MAX.first=tpa;
			MAX.second=d;
		}
	}
	else{
		foo(w+pa[c].first,v+pa[c].second,c+1,d+1);
		foo(w,v,c+1,d);
	}
	return 0;
}

int main(){
	int e=1;
	while(cin>>m,m!=0){
		cout<<"Case "<<e++<<":\n";
		tpa.first=0;
		tpa.second=0;
		MAX.first=tpa;
		MAX.second=0;
		cin>>n;
		rep(i,n){
			int t1,t2;
			cin>>t1>>t>>t2;
			pair <int,int> tm;
			tm.first=t2;
			tm.second=t1;
			pa.push_back(tm);
		}
		//rep(i,pa.size())cout<<pa[i].first<<" "<<pa[i].second<<endl;
		foo(0,0,0,0);
		tpa=MAX.first;
		cout<<tpa.second<<endl<<tpa.first<<endl;
		pa.clear();
	}
	return 0;
}