#include<iostream>
#include<stdio.h>
#include<string.h>

int dp[1005][1005];
int W;
int n;
int v[1005], w[1005];
int now = 1;

int main()
{
	while( 1 ){
		scanf( "%d", &W );
		if( !W ) break;
		scanf( "%d", &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 <= W; j++ ){
				if( j < w[i] ){
				 	dp[i+1][j] = dp[i][j];
				}
				else{
				 	dp[i+1][j] = std::max( dp[i][j], dp[i][j-w[i]] + v[i] );
				}
			}
		}
		
		int res = W;
		for( int i = W; i >= 0; i-- ){
			if( dp[n][i] == dp[n][W] ) res = std::min( res, i );
			else break;
		}
		
		printf( "Case %d:\n", now );
		printf( "%d\n", dp[n][W] );
		printf( "%d\n", res );
		
		now++;	
	}
	

 	return 0;
}