#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int dp[1000][1000];
	int count=1,i,j,W,w,n;
	while(1)
	{
		memset(dp,0,sizeof(dp));
		cin >> W;
		if(W==0)break;
		cin >> n;
		int v[n],w[n];
		for(i=0;i<n;i++)
		{
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<=W;j++)
			{
				if(w[i]>j)
					dp[i+1][j]=dp[i][j];
				else
					dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+v[i]);
			}
		}
		for(i=W;i>=0;i--)
		{
			if(dp[n][W]>dp[n][i])break;
		}
		cout << "Case " << count << ':' << endl << dp[n][W] << endl;
		cout << i+1 << endl;
		count++;
	}
	return 0;
}