#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int w[1000],v[1000];
int memo[1001][1001];
int dfs(int pos,int win){
    if(memo[pos][win]!=-1)return memo[pos][win];
    int res;
    if(pos<1)res=0;
    else if(win-w[pos-1]<0)res=dfs(pos-1,win);
    else res=max(dfs(pos-1,win),dfs(pos-1,win-w[pos-1])+v[pos-1]);
    return memo[pos][win]=res;
}
int main(){
    int W;
    for(int cnt=0;scanf("%d",&W),W;cnt++){
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)scanf("%d,%d",v+i,w+i);
        fill_n(*memo,1001*1001,-1);
        int ans=dfs(n,W);
        printf("Case %d:\n",cnt+1);
        for(int i=W-1;i>=0;i--){
            if(ans==dfs(n,i))continue;
            printf("%d\n%d\n",ans,i+1);
            break;
        }

    }
    return 0;
}