#include<iostream>
#include<queue>
#include<map>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define fr first
#define sc second
#define INF 100
typedef pair<int,int>Pt;
int main(){
  int dp[1001],s[1001],g[1001];
  int n,W,a = 1;
  while(cin >> n && n){
    cin >> W;
    for(int i=0;i<W;i++){
      scanf("%d,%d",&s[i],&g[i]);
    }
    memset(dp,-1,sizeof(dp));
    dp[0] = 0; //Empty
    for(int i=0;i<W;i++){
      for(int j = n-g[i];j>=0;j--){
        if(dp[j] >= 0) dp[j+g[i]] = max(dp[j+g[i]],dp[j]+s[i]);
      }
    }
    int p=0,q=0;
    for(int i=0;i<=n;i++){
      if(dp[i] > p){
        p = dp[i];
        q = i;
      }
    }
    cout << "Case " << a++ << ":" << endl << p << endl << q << endl;
  }
}