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

#define REP(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define REPT(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )
#define REPR(X, Y) for( int (X) = (Y); (X) >= 0; --(X) )

int w, n, u[1000], v[1000];
int dp[1001][1000];
int dataset;

int main(){
	while (true){
		scanf("%d", &w);
		if (!w)break;
		scanf("%d", &n);
		++dataset;
		REP(i, n)
			memset(dp[i], -1, (w + 1)*sizeof(int) / sizeof(char)),
			scanf("%d,%d", &v[i], &u[i]);
		dp[0][0] = 0;
		REP(i, n){ //i番目の要素について検証
			REP(j, w+1){
				if (dp[i][j] == -1)continue;
				dp[i + 1][j] = dp[i][j];
				if (j + u[i] > w)continue;
				dp[i + 1][j + u[i]] =
					std::max(
					dp[i + 1][j] + v[i],
					dp[i][j + u[i]]
					);
			}
		}
		int maxu = [&]()->int{
			REPR(i, w)
				if (dp[n][i] != -1)return i;
		}();
		printf("Case %d:\n%d\n%d\n", dataset, dp[n][maxu], maxu);
	}
	return 0;
}