#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[1002];
int W,w[1001],v[1001],n,cnt = 1,wm;
int main(){
  while(1){
    memset(dp, 0, sizeof(dp));
    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 < n; i++){
      dp[w[i]] = v[i];
      for(int j = W; j >= 0; j--){
	if(j + w[i] <= W) dp[j + w[i]] = max(dp[j + w[i]],dp[j] + v[i]);
      }
    }
    wm = 0;
    for(int i = 0; i <= W; i++){
      if(dp[i] > dp[wm]) wm = i;
    }
    cout << "Case " << cnt <<  ":" << endl;
    cout << dp[wm] << endl;
    cout << wm << endl;
    cnt++;
  }
}