import std.stdio;
import std.string;
import std.conv;
import std.array;

const int W_MAX = 1000;
const int N_MAX = 1000;


void main()
{
    int w, n, cycle = 0;
    
    while ((w = readln.chomp.to!int) != 0) {
        int[W_MAX+1][N_MAX+1] dp = [0];
        
        n = readln.chomp.to!int;
        
        foreach (i; 0..n) {
            // item[0]..value, item[1]..weight
            auto item = readln.split(",").to!(int[]);
            
            foreach (j; 0..(w+1)) {
                if (j - item[1] >= 0)
                    dp[j][i+1] = (dp[j][i] >= (dp[j-item[1]][i] + item[0])) ? dp[j][i] : dp[j-item[1]][j] + item[0];
                else
                    dp[j][i+1] = dp[j][i];
            }
        }
        
        writefln("Case %d:", ++cycle);
        foreach (i; 0..(w+1)) {
            if (dp[i][n] == dp[w][n]) {
                writeln(dp[i][n], "\n", i);
                break;
            }
        }
    }
}