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

const int MAXN = 1004;
const int MAXW = 1004;
int W;
int N;
int P[MAXN];
int C[MAXN];
int dp[MAXW];

int main() {
  while(scanf("%d", &W) != EOF && W) {
    scanf("%d", &N);
    for(int i = 0; i < N; ++i) {
      scanf("%d,%d", &P[i], &C[i]);
    }
    fill(dp,dp+MAXW,0);
    for(int i = 0; i < N; ++i) {
      for(int j = W-C[i]; j >= 0; --j) {
	dp[j+C[i]] = max(dp[j+C[i]], dp[j] + P[i]);
      }
    }
    int res = -1;
    int w = 0;
    for(int j = 0; j <= W; ++j) {
      if(dp[j] > res) {
	res = dp[j];
	w = j;
      }
    }
    static int tc = 0;
    cout << "Case " << ++tc << ":" << endl;
    cout << res << endl;
    cout << w << endl;
  }
  return 0;
}