cnt=0

while True:

    cnt+=1

    W=int(eval(input()))

    if W==0:

        break

    n=int(eval(input()))

    dp=[0]*(W+1)

    for i in range(n):

        v,w=list(map(int,input().split(',')))

        for j in range(W,w-1,-1):

            dp[j]=max(dp[j],dp[j-w]+v)

    wgt=0

    for i in range(W+1):

        if max(dp)==dp[i]:

            wgt=i

            break

    print(("Case {0}:".format(cnt)))

    print((max(dp)))

    print(wgt)
cnt=0

while True:

    cnt+=1

    W=int(eval(input()))

    if W==0:

        break

    n=int(eval(input()))

    dp=[0]*(W+1)

    for i in range(n):

        v,w=list(map(int,input().split(',')))

        for j in range(W,w-1,-1):

            dp[j]=max(dp[j],dp[j-w]+v)

    wgt=0

    md=max(dp)

    for i in range(W+1):

        if md==dp[i]:

            wgt=i

            break

    print(("Case {0}:".format(cnt)))

    print(md)

    print(wgt)
cnt = 0
while True:
    cnt += 1
    W = int(eval(input()))
    if W == 0:
        break
    n = int(eval(input()))
    dp = [0] * (W + 1)
    for i in range(n):
        v, w = list(map(int, input().split(",")))
        for j in range(W, w - 1, -1):
            dp[j] = max(dp[j], dp[j - w] + v)
    wgt = 0
    for i in range(W + 1):
        if max(dp) == dp[i]:
            wgt = i
            break
    print(("Case {0}:".format(cnt)))
    print((max(dp)))
    print(wgt)

cnt = 0
while True:
    cnt += 1
    W = int(eval(input()))
    if W == 0:
        break
    n = int(eval(input()))
    dp = [0] * (W + 1)
    for i in range(n):
        v, w = list(map(int, input().split(",")))
        for j in range(W, w - 1, -1):
            dp[j] = max(dp[j], dp[j - w] + v)
    wgt = 0
    md = max(dp)
    for i in range(W + 1):
        if md == dp[i]:
            wgt = i
            break
    print(("Case {0}:".format(cnt)))
    print(md)
    print(wgt)

+    md = max(dp) -        if max(dp) == dp[i]: +        if md == dp[i]: -    print((max(dp))) +    print(md)