#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;

int dp[1001];
int main() {
  int w,n,cnum=0;
  while(cin>>w, w) {
    cin>>n;
    vector<int> chika(n),miu(n);
    for(int i=0; i<n; ++i) scanf("%d,%d", &chika[i], &miu[i]);
    for(int i=0; i<1001; ++i) dp[i] = 0;


    for(int i=0; i<n; ++i) {
      for(int j=0; j<1001; ++j) {
	if(j + chika[i] < w) continue;
	if(j == 0 || dp[j] > 0) dp[j+miu[i]] = dp[j] + chika[i];
      }
    }

    int maxc = 0,mi = -1;
    for(int j=w; j>=0; --j) {
      if(maxc <= dp[j]) {
	mi = j;
	maxc = dp[j];
      }
    }

    cout<<"Case "<<++cnum<<":"<<endl;
    cout<<maxc<<endl;
    cout<<mi<<endl;
  }
}