#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
int dp[1010][1010] = {{0}};
int W,n;
int w[1010] = {0},p[1010] = {0};
int cnt = 0;
int wm = 0,nc;
int main()
{
    while(1){
        cin >> W;
        if(W == 0) break;
        cnt++;
        cin >> n;
        for(int i = 0; i < n; i++) scanf("%d,%d",&p[i],&w[i]);
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= W; j++){
                if(j < w[i]) dp[i+1][j] = dp[i][j];
                else dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+p[i]);
            }
        }
        nc = W;
        while(nc >= 0){
            if(dp[n][nc] == dp[n][W])
                wm = nc;
            nc--;
        }
        cout << "Case " << cnt << ':' << endl << dp[n][W] << endl << wm << endl;
    }
	return 0;
}