#include<cstdio>
#include<algorithm>
#define in(a) scanf("%d",&a)
using namespace std;
int main(){
  int w,n,a,b,ca=0;
  
  while(in(w) && w != 0){
    in(n);
    int dp[n][w+1];
    int wait[n][w+1];
    fill_n(*dp,n*(w+1),0);
    fill_n(*wait,n*(w+1),0);
    for(int i = 0;i < n;i++){
      scanf("%d,%d",&a,&b);
      if(i == 0){
	dp[i][b] = a;
	wait[i][b] = b;
      }
      else {
	for(int j = 0;j <= w;j++){
	  if(j < b){
	    dp[i][j] = dp[i-1][j];
	    wait[i][b] = wait[i-1][b];
	  }
	  else if(dp[i-1][j] > dp[i-1][j-b]+a){
	    dp[i][j] = dp[i-1][j];
	    wait[i][j] = wait[i-1][j];
	  }
	  else if(dp[i-1][j] <= dp[i-1][j-b]+a){
	    dp[i][j] = dp[i-1][j-b]+a;
	    wait[i][j] = wait[i-1][j-b]+b;
	  } 
	}
      }
    }
    ca++;
    printf("Case %d:\n",ca);
    printf("%d\n%d\n",dp[n-1][w],wait[n-1][w]);
  }

  return (0);
}