#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  int W, N, v, w, count = 1;
  char c;
  while (true) {
    cin >> W;
    if (W == 0) return 0;
    cin >> N;

    ll dp[N + 1][W + 1];
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= W; j++)
        dp[i][j] = 0;

    for (int i = 0; i < N; i++) {
      cin >> v >> c >> w;
      for (int j = W; j >= 0; j--) {
        // 入れないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        // 入れるパターン
        if (j - w >= 0) dp[i + 1][j - w] = max(dp[i + 1][j - w], dp[i][j] + v);
      }
    }

    int res = 0, pos = 0;
    for (int j = W; j >= 0; j--) {
      if (res < dp[N][j]) {
        res = dp[N][j];
        pos = j;
      }
    }
    cout << "Case " << count << ":" << endl;
    cout << res << endl;
    cout << W - pos << endl;
    count++;
  }
}

