#include<iostream>
#include<cstdio>
int max();
using namespace std;
int main()
{
    int W,n,w[1001],v[1001];
    int cnt = 1;
    int dp[1024][1000] = {{0}};
    for(;;){
        scanf("%d",&W);
        if(W == 0)break;
        scanf("%d",&n);
        for(int i = 1;i <= n; i++)scanf("%d,%d",&v[i],&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]+v[j]);
                }
            }
        }
        int x = 0;
        for(int i = 0;i <= W; i++){
            if(dp[i][n] == dp[W][n]){
                x = i;
                break;
            }
        }
        printf("Case %d:\n",cnt);
        printf("%d\n",dp[W][n]);
        printf("%d\n",x);
        cnt++;
    }
    return 0;
}
int max(int a,int b){
    int x;
    x = (a > b) ? a:b;
    return x;
}