#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SUPmaxm 1001
#define SUPsupw 10001

int maxm;
int supw;
int vs[SUPmaxm];
int wos[SUPmaxm];
int dp[SUPmaxm][SUPsupw]; //m ika no item tukatte, w ika no omomi de, sum v no max
int rev,rew;

void solve(){
	FOR(m,0,maxm+1) FOR(w,0,supw) dp[m][w]=0;
	FOR(m,1,maxm+1){
		FOR(w,0,wos[m]) dp[m][w] = dp[m-1][w];
		FOR(w,wos[m],supw) dp[m][w] = max(dp[m-1][w],dp[m-1][w-wos[m]]+vs[m]);
	}
}

void findRe(){
	rew = supw-1;
	rev = dp[maxm][rew];
	while(true){
		if(dp[maxm][--rew]!=rev){++rew;break;}
	}
}



int main(){
	for(int casei=1;true;++casei){
		cin >> supw;			if(!supw) return 0;
		cin >> maxm;
		FOR(m,1,maxm+1) scanf("%d,%d",vs+m,wos+m);
		solve();
		findRe();
		cout << "Case " << casei << ":" << endl;
		cout<<rev<<endl;
		cout<<rew<<endl;
	}
}