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

void main()
{
    for(int t=1; ; ++t)
    {
        immutable W = readln().chomp().to!int();
        if(!W) break;
        auto dp = new int[W+1];
        foreach(_;0..readln().chomp().to!int())
        {
            auto m = readln().chomp().split(",").map!(to!int);
            immutable v=m[0], w=m[1];
            foreach_reverse(i;w..W+1)
                dp[i]=max(dp[i],dp[i-w]+v);
        }
        int mv=0,mw=0;
        foreach(int w,v;dp)
            if(mv<v) mv=v,mw=w;
        writeln("Case ",t,":\n",mv,"\n",mw);
    }
}