#include<bits/stdc++.h>
using namespace std;

int W,n,v,w;
int dp[1001],casenum;

int main(){
  casenum = 1;
  while(scanf("%d",&W), W){
    scanf("%d",&n);
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++){
      scanf("%d,%d",&v,&w);
      for(int i=W;i>=0;i--){
	if(i+w <= W){
	  dp[i+w] = max(dp[i+w], dp[i] + v);
	}
      }
    }

    int ansv = 0, answ = 0;
    for(int i=0;i<=W;i++){
      if(ansv < dp[i]){
	ansv = dp[i]; answ = i;
      }
    }
    printf("Case %d:\n%d\n%d\n",casenum, ansv, answ);
    casenum++;
  }
}