#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define MAX 1010

typedef long long ll;

int W,N;
int w[MAX],v[MAX];
ll dp[MAX][MAX];

int main(){
  int i,j,cnt=0;

  for(;;){
    scanf("%d",&W); if(W == 0) break;
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",&v[i],&w[i]);

    rep(i,MAX) rep(j,MAX) dp[i][j] = 0;

    int res;
    for(i = 0; i <= N; i++){
      for(j = 0; j <= W; j++){
        if(j - w[i] < 0) dp[i+1][j] = dp[i][j];
        else {
          if(dp[i][j] < dp[i][j-w[i]] + v[i]){
            dp[i+1][j] = dp[i][j-w[i]] + v[i];
            res = j;
          }
          else dp[i+1][j] = dp[i][j];
        }
      }
    }

    printf("Case %d:\n%lld\n%d\n",++cnt,dp[N][W],res);
  }

  return 0;
}