#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;


int main() {
  int dp[2][1001],n,k[1000],w[1000],i,j,h,kg,mk,mw,cn=0;
  while(true) {
	  cin >> kg;
	  if (kg==0) break;
	  for (i=0;i<=kg;i++) {dp[0][i]=0; dp[1][i]=0;}
	  cin >> n;
	  for (i=0;i<n;i++) scanf("%d,%d",&k[i],&w[i]);
	  for (i=0,j=0;i<n;i++) {
		  j=1-j;
		  for (h=kg;h>=0;h--)
			  if (h+w[i]>kg) dp[j][h]=dp[1-j][h];
			  else dp[j][h]=(dp[1-j][h+w[i]]+k[i])>dp[1-j][h] ? dp[1-j][h+w[i]]+k[i] : dp[1-j][h];
	  }
  mk=0;
  for (i=kg;i>=0;i--) if (mk<dp[j][i]) {mk=dp[j][i]; mw=i;}
  cn++;
  cout << "Case " << cn << ":" << endl;
  cout << mk << endl;
  cout << kg-mw << endl;
  }
 
  return 0;
}