#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

int main(){
	int w, n, ca = 0;
	while(cin >>w){
		if(w == 0) break;
		ca++;
		int prev[1000], prew[1000], ans = 0, ansc = 0, anscnt = 0;
		cin >>n;
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &prev[i], &prew[i]);
		}
		int s = 0, lim = 0;
		while(s < n){
			queue<int> v;
			int cnt = 0;
			for(int j = s; j <= lim; j++){
				v.push(j);
				cnt += prew[j];
			}
			if(lim >= n - 1){
				s++;
				lim = s;
			} else{
				lim++;
			}
			if(cnt > w) continue;
			int sum = 0, c = 0;
			while(!v.empty()){
				int a = v.front();
				sum += prev[a];
				v.pop();
				c++;
			}
			if(ans < sum || (ans == sum && c > ansc)){
				ans = sum;
				anscnt = cnt;
			}
		}
		cout <<"Case " <<ca <<":" <<endl <<ans <<endl <<anscnt <<endl;
	}
	return 0;
}