#include<iostream>
#include<stdio.h>
using namespace std;
int dp[1005][1005];
int main(){
  int W,N,v[1000],w[1000];
  int n=1;
  int lastj=0;
  char jyama;
  while(cin>>W&&W){
    cin>>N;
    for(int i=0;i<N;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    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]);
	  if(j>0&&dp[i][j]!=dp[i][j-1]) lastj=j;
	}
      }
    }    
    cout<<"Case "<<n<<":"<<endl;
    cout<<dp[N][W]<<endl;
    cout<<lastj<<endl;
    ++n;
  }
}