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

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


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