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

int n, w, value[1000], weight[1000], memo[1000][1000];
int dp(int val, int wei);

int main(){
  int data = 1;
  while(cin >> w, w){
    memset(memo, 0, sizeof(memo));
    cin >> n;
    for(int i = 0; i < n; i++){
      scanf("%d,%d", &value[i], &weight[i]);
    }

    int wgtsum = 0;
    int maxi = dp(0, w);
    for(int i = n-1; i >= 0 && !maxi; i++){
      for(int j = n-1; j >= 0 && !maxi; j++){
	if(memo[i][j] == maxi) wgtsum = i;
      }
    }
      
    cout << "Case " << data << ":" << endl;
    cout << maxi << endl;
    cout << w - wgtsum - 1 << endl;
    data++;
  }
}

int dp(int val, int wei){
  if(memo[wei][val]) return memo[wei][val];
  if(val >= n) return 0;
  if(wei < weight[val]) return memo[wei][val] = dp(val+1, wei);
  else return memo[wei][val] = max(dp(val+1, wei-weight[val])+value[val], dp(val+1, wei));
}