#include <cstdio>
#include <cstring>
#include <algorithm>
#include <limits.h>
using namespace std;

typedef pair<int, int> P;

bool compare(const P &l, const P &r) {
  return l.second > r.second;
}

int max(int a, int b) {
  return (a > b) ? a : b;
}

int min(int a, int b) {
  return (a < b) ? a : b;
}

int main() {
  int w, n, c=0;
  P x[1000];
  int buf1[1001];
  int buf2[1001];
  int *p1, *p2, *tmp;
  int a, b;

  while (scanf("%d", &w) && w) {
    ++c;
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
      scanf("%d,%d", &x[i].first, &x[i].second);
    }
    sort(x, x+n, compare);
    memset(buf1, 0, sizeof(int)*(w+1));
    memset(buf2, 0, sizeof(int)*(w+1));
    p1 = buf1;
    p2 = buf2;
    for (int i=0; i<n; i++) {
      int t = w-x[i].second;
      for (int j=0; j<=w; j++) {
        if (p1[j]) {
          p2[j] = max(p1[j], p2[j]);
          if (j<=t) p2[j+x[i].second] = max(p1[j+x[i].second], p1[j]+x[i].first);
        }
      }
      p2[x[i].second] = max(p1[x[i].second], x[i].first);
      tmp = p1;
      p1 = p2;
      p2 = tmp;
    }
    a = 0;
    for (int i=0; i<=w; i++) {
      a = max(a, p1[i]);
    }
    b = INT_MAX;
    for (int i=0; i<=w; i++) {
      if (p1[i]==a) {
        b = min(b, i);
      }
    }
    printf("Case %d:\n", c);
    printf("%d\n", a);
    printf("%d\n", b);
  }
  return 0;
}