#include <bits/stdc++.h>
using namespace std;

void solve( int case_i, int W, int N ) {
	vector<int> v( N+1 ), w( N+1 );
	for( int i = 1; i <= N; ++i ) {
		char c;
		cin >> v[i] >> c >> w[i];
	}

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

	int rv = dp[N][W];
	int rw = W;
	for( int i = W-1; i > 0; --i ) {
		if( dp[N][i] == rv ) {
			rw = i;
		}
	}

	cout << "Case " << case_i << ":\n";
	cout << rv << '\n' << rw << '\n';
}

int main() {
	ios_base::sync_with_stdio( false );

	int W, N;
	for( int i = 1; cin >> W >> N; ++i ) {
		if( W == 0 ) {
			break;
		}

		solve( i, W, N );
	}

	return 0;
}

