#include<iostream>
#include<algorithm>
using namespace std;
#define INF ( 1 << 25 )
int main(){
  int W, N, v[1000], w[1000], dp[1001], cnt = 0;
  char op;
  while(cin >> W >> N , W){
    fill_n(dp,1001,0);
    for(int i = 0 ; i < N ; i++ ) cin >> v[i] >> op >>  w[i];
    for(int i = 0 ; i < N ; i++ ){
      for(int j = W ; j >= w[i] ; j-- ){
        dp[j] = max( dp[j], dp[j - w[i]] + v[i]);
      }
    }
    int ret = 0;
    for(int i = 0 ; i <= W ; i++ ){
      if(dp[i] > dp[ret]) ret = i;
    }
    cout << "Case " << ++cnt << ":" << endl << dp[ret] << endl << ret << endl;
  }
}