#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int dp[10000][10000];

int main(){
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while(scanf("%d", &W) && W){
    c++;
    scanf("%d", &N);
    for(int i = 0; i < N; ++i) scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i < N; ++i){
      for(int j = 0; j <= W; j++){
	if(j < w[i]) dp[i + 1][j] = dp[i][j];
	else dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for(int j = 0; j < W; ++j) if(dp[N][j] < dp[N][W]) res++;
    printf("Case %d:\n%d\n%d\n", c, dp[N][W], res);
  }
  return 0;
}