#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
	int w;
	int c = 1;

	while ( true )
	{
		cin >> w;

		if ( w == 0 )
		{
			break;
		}
		
		int n;
		cin >> n;
		cin.ignore( 1 );

		vector<int> price, weight;

		for ( int i = 0; i < n; i++ )
		{
			string buffer;
			
			cin >> buffer;

			for ( unsigned int i = 0; i < buffer.length(); i++ )
			{
				if ( buffer[i] == ',' )
				{
					buffer[i] = ' ';
					break;
				}
			}

			int input;
			istringstream iss( buffer );
			
			iss >> input;
			price.push_back( input );

			iss >> input;
			weight.push_back( input );
		}

		vector< vector<int> > dp;
		dp.push_back( vector<int>() );

		dp[0].resize( w + 1 );
		fill( dp[0].begin(), dp[0].end(), 0 );

		for ( unsigned int i = 1; i <= (unsigned)n; i++ )
		{
			dp.push_back( vector<int>() );
			dp[i].resize( w + 1 );
			fill( dp[i].begin(), dp[i].end(), 0 );

			for ( unsigned int j = 0; j <= (unsigned)w; j++ )
			{
				if ( weight[i] <= (int)j )
				{
					dp[i][j] = max( dp[i-1][j-weight[i]] + price[i], dp[i-1][j] );
				}
				else
				{
					dp[i][j] = dp[i-1][j];
				}
			}
		}
		
		vector<int> result = *dp.rbegin();

		unsigned int res = 0;
		int m = result[0];

		for ( unsigned int i = 1; i < result.size(); i++ )
		{
			if ( m < result[i] )
			{
				m = result[i];
				res = i;
			}
		}
		
		cout << "Case " << c++ << ":" << endl;
		cout << m << endl;
		cout << res << endl;
	}

	return 0;
}