#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;

int main(){

  int m,n;
  int v[1001],w[1001];
  int dp[1001][1001];
  int cnt = 0;
  int d = 0;

  while(cin >> m, m){
    cin >> n;

    for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);

    memset(dp,0,sizeof(dp));

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

    for(int i=m;i>=0;i--){
      if(dp[n][i] == dp[n][m]) d = i;
    }

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