#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define latte WORLD

int N,W,tv[1000],tw[1000];
int memo[1000][1001],ansv,answ;

int latte(int n,int v,int w){
    if(w>W) return 0;
    if(n==N){
        if(ansv<v || (ansv==v && answ>w)){
            ansv = v; answ = w;
        }
        return v;
    }
    if(memo[n][w]!=-1) return memo[n][w];
    int ans=0;
    memo[n][w] = max(latte(n+1,v+tv[n],w+tw[n]),latte(n+1,v,w));
    return memo[n][w];
}

int main(){
    for(int ds=1;cin>>W,W;ds++){
        cin>>N;
        for(int i=0;i<N;i++) scanf("%d,%d",&tv[i],&tw[i]);
        fill_n((int*)memo,1000*1001,-1); ansv = answ = 0;
        latte(0,0,0);
        printf("Case %d:\n%d\n%d\n",ds,ansv,answ);
    }
}