#include <cstdio>
#include <algorithm>

using namespace std;

typedef int weight_t;
int W,N;

int tbl[2][1001];

int main(){
    int round=0;
    while(scanf("%d",&W)&&W!=0){
        round++;
        scanf("%d",&N);
        //初期化
        for(int i=0;i<=W;i++)
            tbl[0][i] = 0, tbl[1][i] = 0;
        tbl[0][0]=0;



        //動的計画法
        for (int i=0;i<N;i++){
            int v,w;
            scanf("%d,%d",&v,&w);

            // printf("%d",i);
            int index=i%2;
            //jはweightのループ変数
            for (int j=0;j<=W;j++){
                int next_index=(index+1)%2;
                //宝物をとらない選択肢
                tbl[next_index][j] = max(tbl[next_index][j], tbl[index][j]);

                //宝物をとる選択肢
                int new_weight=j+w;
                if (new_weight<=W){
                    tbl[next_index][new_weight]=max(tbl[next_index][new_weight],tbl[index][j]+v);
                }

            }

            // for (int k=0;k<)
        }

        int final_index = N % 2;
        int value = 0, weight = 0;
        for (int i = 0; i <= W; i++)
        {
            if (tbl[final_index][i] > value)
            {
                value = tbl[final_index][i];
                weight = i;
            }
        }

        printf("Case %d:\n%d\n%d\n", round, value, weight);
    }
    return 0;
}
