#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX_N 1000
#define MAX_W 1000

int N, W;
int v[MAX_N], w[MAX_N];
int memo[MAX_N][MAX_W];
int resW;

int solve(int index, int weight){
  if(memo[index][weight] > 0) return memo[index][weight];

  int res;
  if(index == N) res = 0;
  else if(weight < w[index]) res = solve(index + 1, weight);
  else{
    res = max(solve(index + 1, weight), solve(index + 1, weight - w[index]) + v[index]);
  }
  return memo[index][weight] = res;
}

void askw(int ans, int weight, int index){
  if(ans == 0){
    if(weight < resW) resW = weight;
  }
  if(index == N) return;
  if(ans - v[index] >= 0){
    askw(ans - v[index], weight + w[index], index + 1);
    askw(ans, weight, index + 1);
  }
}

int main(void){
  int cnt = 0, ans;
  while(cin >> W, W){
    cnt++;
    cin >> N;
    for(int i = 0; i < N; i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    resW = 1e9;
    memset(memo, -1, sizeof(memo));
    ans = solve(0, W); askw(ans, 0, 0);
    cout << "Case " << cnt << ":" << endl;
    cout << ans << endl;
    cout << resW << endl;
  }
  return 0;
}