#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
int dp[1111][1111];
int w[1111],v[1111];
int main(){
  int W,N;
  int c = 1;
  while(1){
    cin >> W;
    if(!W)break;
    cin >> N;
    for(int i=0;i<N;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    memset( dp , 0 , sizeof(dp) );
    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 ret=0,value=0;
    for(int i=0;i<=W;i++){
      if(value<dp[N][i]){
	ret = i;
	value = dp[N][i];
      }
    }
    cout << "Case" << c++ << ":" <<  endl;
    cout << dp[N][W] << endl;
    cout << ret << endl;
  }
}