#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[1000], w[1000], v[1000], a, n, t = 0, i, j, mv;
int main() {
  while (cin >> a, a) {
    t++;
    cin >> n;
    fill(dp, dp + a + 1, 0);
    for (i = 0; i < n; i++)
      scanf("%d,%d", &v[i], &w[i]);
    for (i = 0; i < n; i++)
      for (j = a; j >= 0; j--)
        if (j + w[i] <= a) dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
    mv = 0;
    for (i = 0; i <= a; i++)
      if (dp[i] > dp[mv]) mv = i;
    cout << "Case " << t << ':' << endl << dp[mv] << endl << mv << endl;
  }
}