#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std ;

int N , W ;
int v[1001] , w[1001] ;
int dp[1002][1002] ;
int ans1 , ans2 ;

void solve(){
	memset(dp,-1,sizeof(dp)) ;
	dp[0][0] = 0 ;
	ans1 = 0 ;
	ans2 = 0 ;
	
	for( int i=1 ; i<=N ; i++ ){
		for( int j=0 ; j<=W ; j++ ){
			if( dp[i-1][j] >= 0 ){
				if( dp[i][j] < dp[i-1][j] ) dp[i][j] = dp[i-1][j] ;
				if( j+w[i] < W ){
					dp[i][j+w[i]] = max( dp[i-1][j+w[i]] ,dp[i-1][j] + v[i] ) ;
				}
			}
		}
	}
	
	for( int i=0 ; i<W ; i++ ){
		if( ans1 < dp[N][i] ){
			ans1 = dp[N][i] ;
			ans2 = i ;
		}
	}
}

int main(){
	
	int t=0 ;
	
	while( cin >> W >> N , W ){
		for( int i=1 ; i<N ; i++ ){
			scanf("%d,%d" ,&v[i] ,&w[i] ) ;
		}t++ ;
		solve() ;
		
		cout << "Case " << t << ':' << endl ;
		cout << ans1 << endl ;
		cout << ans2 << endl ;
	}
	
return 0 ; 
}