#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  int W, N;
  int v[1001], w[1001];
  for(int t = 1; scanf("%lld", &W), W; t++) {
    scanf("%lld", &N);
    rep(i, N) scanf("%lld,%lld", &v[i], &w[i]);

    int dp[1001] = {0};
    for(int i = 0; i < N; i++) {
      for(int j = W; j >= w[i]; j--) {
	dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
      }
    }

    int aw = 0;
    while(dp[aw] != dp[W]) aw++;

    printf("Case %d:\n%d\n%d\n", t, dp[aw], aw);
  }

  return 0;
}