#include<cstdio>
using namespace std;
int field[1001][1001];
int main(void){
    int w;
    int ans_e,ans_w;
    int c = 0;
    while(scanf("%d",&w),w){
        ans_e = 0;
        ans_w = 0;
        c++;
        int i_exp[1001];
        int i_cos[1001];
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d,%d",&i_exp[i],&i_cos[i]);
        }
        for(int i = 1;i <= n;i++){
            for(int j = 0;j <= w;j++){
                field[i][j] = field[i-1][j];
                if(j - i_cos[i] >= 0 && field[i][j] <= field[i-1][j-i_cos[i]]+i_exp[i]) field[i][j] = field[i-1][j-i_cos[i]]+i_exp[i];
            }
        }
        ans_e = field[n][w];
        for(int i = w;i >= 0;i--){
            if(ans_e != field[n][i]){
                ans_w = i+1;
                break;
            }
        }
        printf("Case %d:\n%d\n%d\n",c,ans_e,ans_w);
    }
    return 0;
}