#include <cstdio>
#include <algorithm>
using namespace std;
int N, W, maxW;
int w[1001], v[1001];
int dp[1001][1001];
int c;

void solve() {
    for (int i = 0; i <= W; i++) dp[N][i] = 0;
    for (int i = 0; i < N; i++) dp[i][0] = 0;

    for (int i = N - 1; i >= 0; i--) {
        for (int j = 0; j <= W; j++) {
            if (j < w[i]) dp[i][j] = dp[i+1][j];
            else dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
        }
    }
    maxW = W;
    for (int i = N - 1; i >= 0; i--) {
        for (int j = 0; j <= W; j++) if (dp[i][j] == dp[0][W]) maxW = min(maxW, j);

    }
    printf("Case %d:\n", c);
    printf("%d\n%d\n", dp[0][W], maxW);
}

int main()
{
    c = 1;
    for (; ;) {
        scanf("%d", &W);
        if (W == 0) break;
        scanf("%d", &N);
        for (int i = 0; i < N; i++) scanf("%d,%d", &v[i], &w[i]);
        solve();
        c++;
    }
    return 0;
}

