#include <iostream>
#define MAX_N 1000
#define MAX_W 10000

using namespace std;

main(){
  int W, N;
  int v[MAX_N], w[MAX_N];
  int dp[MAX_N+1][MAX_W+1];

  for(int count=1;;count++){
    cin >> W;
    if(W==0) break;

    cin >> N;
    for(int i=0;i<N;i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    for(int i=0;i<=W;i++){
      dp[0][i]=0;
    }
    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]);
	}
      }
    }
    int ans;
    for(int i=0;i<=W;i++){
      if(dp[N][W]==dp[N][i]){
	ans=i;
	break;
      }
    }
    printf("Case %d:\n", count);
    cout << dp[N][W] << endl;
    cout << ans << endl;
  }
  return 0;
}