#include <cstdio>
#include <algorithm>
#define MAX_N 1000
#define MAX_W 1000
using namespace std;
int W;
int N; 

int w[MAX_N]; 
int v[MAX_N]; 
int dp[MAX_N + 1][MAX_W + 1];
bool input() 
{
  scanf("%d", &W);
  if(!W) return false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) 
  {
    scanf("%d,%d", &v[i], &w[i]);
  }
  return true;
}
void solve() 
{

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

int main() 
{

  int cnt = 0;

  while (input())
  {
    solve();
    int i;
    for (i = W; i >= 0; i--) 
    {
      if (dp[N][W] != dp[N][i]) break;
    }
    printf("Case %d:\n", ++cnt);
    printf("%d\n", dp[N][W]);
    printf("%d\n", i + 1);
  }

  return 0;
}