#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

int main(void){
  int W,N;
  int cnt = 0;
  while(scanf("%d",&W),W){
    vector<pair<int,int> > v;
    ll dp[1001][1001];
    memset(dp,0,sizeof(dp));
    
    cin>>N;
    
    rep(i,N){
      int s,t;
      scanf("%d,%d",&s,&t);
      v.push_back(make_pair(s,t));
    }
    for(int i = N - 1 ; i >= 0 ; i --){
      for(int j = 0 ; j <= W ; j ++){
	if(j < v[i].second)dp[i][j] = dp[i+1][j];
	else dp[i][j] = max(dp[i+1][j],dp[i+1][j-v[i].second]+v[i].first);
      }
    } 

    printf("Case %d:",++cnt);
    printf("%lld\n",dp[0][W]);
    for(int i = W ; ; i --){
      if(dp[0][i] != dp[0][W]){
	cout<<i+1<<endl;
	break;
      }
    }
  }
}