#include<bits/stdc++.h>
using namespace std;
int dp[1001][1001],wait[1001],value[1001],waiting[1001][1001];
void Initialization()
{
    for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){
                    dp[i][j]=-1;
                    waiting[i][j]=-1;
            }
    }
}
int power(int n)
{
    int num=1;
    for(int i=1;i<=n;i++)num*=10;
    return num;
}
int serch(int i,int j,int n,int co,int w)
{
    int num,wai=w;
    if(co==1)wai-=j;
    if(dp[i][j]>-1)return dp[i][j];
    else if(i==n)num=0;
    else if(j<wait[i])num=serch(i+1,j,n,0,w);
    else num=max(serch(i+1,j,n,0,w),serch(i+1,j-wait[i],n,1,w)+value[i]);
    waiting[i][j]=wai;
    return dp[i][j]=num;
}
int waitserch(int w,int n)
{
    int num=0;
    for(int i=n;i>0;i--){
        for(int j=0;j<=w;j++)if(waiting[i][j]>num&&waiting[i][j]>0&&waiting[i][j]!=w)num=waiting[i][j];
    }
    return num;
}
int main()
{
    int n,w,point,num=1,wai=0;
    string str;
    while(cin>>w&&w!=0){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>str;
            value[i]=0;wait[i]=0;
            for(int j=0;;j++){
                if(str[j]==','){
                    for(int k=0;k<j;k++)value[i]+=power(j-k-1)*(str[k]-'0');
                    point=j;
                }
                if(str[j]=='\0'){
                    for(int k=point+1;k<j;k++)wait[i]+=power(j-k-1)*(str[k]-'0');
                    break;
                }
            }
        }
        Initialization();
        point=serch(0,w,n,0,w);
        wai=waitserch(w,n);
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        num++;
    }
}