#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

int main(void){
    int weigh;
    int w[1001],v[1001];
    int dp[1001][1001];
    int n;
    int c=0;
    
    while(cin>>weigh,weigh!=0){
        for(int i=0;i<1001;i++){
            memset(dp[i],0,1001);
        }
    
        cin>>n;
        for(int i=0;i<n;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
    
        for(int i=0;i<n;i++){
            for(int j=0;j<=weigh;j++){
                if(j<w[i]){
                    dp[i+1][j]=dp[i][j];
                }
                else{
                    dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
                }
            }
        }
        printf("Case %d:\n",++c);
        printf("%d\n",dp[n][weigh]);
        for(int i=0;i<=weigh;i++){
            if(dp[n][weigh]==dp[n][i]){
                printf("%d\n",i);
                break;
            }
        }
    }
    return(0);
}