#include<stdio.h>
int main(void){
    int W,N,v[1000], w[1000], vw[1000], i, j, key, check, sum_v, sum_w;
    scanf("%d %d", &W, &N);
    while ( W ) {
        for ( i = 0; i < N; i++ ) {
            scanf("%d,%d", &v[i], &w[i]);
            vw[i] = v[i] / w[i];
            /*printf("a %d %d\n", N, i);*/
        }
        /*printf("b\n");*/
        check = 1;
        for ( i = 0; check; i++ ) {
            check = 0;
            for ( j = N - 1; j > i; j-- ) {
                if (vw[j] > vw[j - 1] ) {
                    key = vw[j];
                    vw[j] = vw[j - 1];
                    vw[j - 1] = key;
                    key = v[j];
                    v[j] = v[j - 1];
                    v[j - 1] = key;
                    key = w[j];
                    w[j] = w[j - 1];
                    w[j - 1] = key;
                } else if (vw[j] == vw[j - 1] && w[j] < w[j - 1] ) {
                    key = vw[j];
                    vw[j] = vw[j - 1];
                    vw[j - 1] = key;
                    key = v[j];
                    v[j] = v[j - 1];
                    v[j - 1] = key;
                    key = w[j];
                    w[j] = w[j - 1];
                    w[j - 1] = key;
                }
            }
        }
        for ( i = 0; i < N; i++ ) printf("vw%d v%d w%d\n", vw[i], v[i], w[i]);
        sum_v = sum_w = 0;
        for ( i = 0; i < N; i++ ) {
            if ( sum_w + w[i] <= W ) {
                sum_v += v[i];
                sum_w += w[i];
            }
            printf("sum_v%d sum_w%d\n", sum_v, sum_w);
        }
        printf("\n");
        printf("%d\n%d", sum_v, sum_w);
    }
    return 0;
}