#include <iostream>
#include <algorithm>
using namespace std;
int dp[1001][1001]={0};
int main()
{
    int c=0;
    while(1)
    {
        int W;
        cin>>W;if(!W)return 0;
        int n;
        cin>>n;
        int a[1001],w[1001];
        for(int i=1;i<=n;++i){
            cin>>a[i];
            cin.ignore();
            cin>>w[i];
        }
        for(int j=1;j<=n;++j){
            for(int i=0;i<=W;++i){
                if(i<w[j]){
                    dp[i][j]=dp[i][j-1];
                }else{
                    dp[i][j]=max(dp[i][j-1],dp[i-w[j]][j-1]+a[j]);
                }
            }
        }
        c++;
        cout<<"Case"<<" "<<c<<":"<<endl;
        cout<<dp[W][n]<<endl;
        for(int i=0;i<=W;++i){
            if(dp[i][n]==dp[W][n]){
                    cout<<i<<endl;
                    break;
            }
        }
    }
}