#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1500][1500];
int main()
{
	int count=1,i,j,W,n;
	while(1)
	{
		cin >> W;
		if(W==0)return 0;
		scanf("%d",&n);
		memset(dp,0,sizeof(dp));
		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;
		cout << dp[n-1][W] << endl;
		cout << i+1 << endl;
		count++;
	}
	return 0;
}