Harryxun/stf_run huggingface dataset:
n = input()
total = 0
for char in n:
	total = (total + int(char)) % 9
if !total:
	print("Yes")
else:
  	print("No")

### FIXED CODE ###

num = input()
total = 0
for char in num:
    total = (total + int(char)) % 9
if total == 0:
    print("Yes")
else:
    print("No")
from collections import deque

n=int(input())
to=[[] for _ in range(n)]
eda=[]
for _ in range(n-1):
  a,b=map(int,input().split())
  to[a-1].append(b-1)
  to[b-1].append(a-1)
  eda.append([a-1,b-1])
  
c=list(map(int,input().split()))

c.sort(reverse=True)

cd=deque(c)
poi=[0 for _ in range(n)] 
vi=[-1 for _ in range(n)]

vi[0]+=1
def dfs(now):
  vi[now]+=1
  v=cd.popleft()
  poi[now]+=v
  for i in to[now]:
    if vi[i]==-1:
      dfs(i)
  
dfs(0)

ans=0

for u in eda:
  x,y=u
  ans+=min(poi[x],poi[y])
  
print(ans)
for u in range(n):
  poi[u]=str(poi[u])
  
print(' '.join(poi))
      



### FIXED CODE ###

from collections import deque
import sys

sys.setrecursionlimit(10**7)

n=int(input())
to=[[] for _ in range(n)]
eda=[]
for _ in range(n-1):
  a,b=map(int,input().split())
  to[a-1].append(b-1)
  to[b-1].append(a-1)
  eda.append([a-1,b-1])
  
c=list(map(int,input().split()))

c.sort(reverse=True)

cd=deque(c)
poi=[0 for _ in range(n)] 
vi=[-1 for _ in range(n)]

vi[0]+=1
def dfs(now):
  vi[now]+=1
  v=cd.popleft()
  poi[now]+=v
  for i in to[now]:
    if vi[i]==-1:
      dfs(i)
  
dfs(0)

ans=0

for u in eda:
  x,y=u
  ans+=min(poi[x],poi[y])
  
print(ans)
for u in range(n):
  poi[u]=str(poi[u])
  
print(' '.join(poi))
from sys import stdin

N, M, *A = map(int, stdin.read().split())
req = {1: 2,
       2: 5,
       3: 5,
       4: 4,
       5: 5,
       6: 6,
       7: 3,
       8: 7,
       9: 6}

B = sorted([(a, req[a]) for a in A], key=lambda x: x[0], reverse=True)
min_ = min(B, key=lambda x: x[1])[1]


def dp(N, B, min_):
    if N // min_ == 1:
        for a, n in B:
            if n == N:
                return [a]
    else:
        for a, n in B:
            if (n - min_) <= (N % min_):
                return [a] + dp(N-n, B, min_)


lst = dp(N, B, min_)
print(''.join(map(str, lst)))

### FIXED CODE ###

from sys import stdin

N, M, *A = map(int, stdin.read().split())
req = {1: 2,
       2: 5,
       3: 5,
       4: 4,
       5: 5,
       6: 6,
       7: 3,
       8: 7,
       9: 6}

B = sorted([(a, req[a]) for a in A], key=lambda x: x[0], reverse=True)
a_min, n_min = min(B, key=lambda x: x[1])
g = [(a, n-n_min) for a, n in B if a > a_min and n > n_min]
l = [(a, n-n_min) for a, n in B if a < a_min and n > n_min]
l.sort(key=lambda x: x[1], reverse=True)
C = g + l

def dp(rem, C):
    ret = None
    if rem == 0:
        ret = []
    elif rem > 0:
        for i in range(len(C)):
            a, m = C[i]
            nxt = dp(rem-m, C[i:])
            if nxt is not None:
                ret = [a] + nxt
                break
    return ret


i = 0
while True:
    quo = N // n_min - i
    rem = N % n_min + i * n_min
    lst = dp(rem, C)
    if lst is not None:
        lst = sorted(lst + [a_min] * (quo-len(lst)), reverse=True)
        print(''.join(map(str, lst)))
        break
    i += 1
lista=[]
t=0
ch=0
e=0
d=0
tflag=1
while True:
    try:
        lista.append(list(map(int,input().split())))
    except:
        break;

def func01(a,b,c):
    global t
    global ch
    global e
    global d
    if a+b>c:
        t+=1
        if a**2+b**2==c**2:
            ch+=1
        elif a**2+b**2>c**2:
            e+=1
        elif a**2+b**2<c**2:
            d+=1
    else tflag=0
while(tflag==1):
    for i in range(len(lista)):
        x=sorted(lista[i])
        func01(x[0],x[1],x[2])
print(t,ch,e,d)


### FIXED CODE ###

lista=[]
t=0
ch=0
e=0
d=0
tflag=1
while True:
    try:
        lista.append(list(map(int,input().split())))
    except:
        break;

def func01(a,b,c):
    global t
    global ch
    global e
    global d
    global tflag
    if a+b>c:
        t+=1
        if a**2+b**2==c**2:
            ch+=1
        elif a**2+b**2>c**2:
            e+=1
        elif a**2+b**2<c**2:
            d+=1
    else:
        tflag=0
        
i=0
for i in range(len(lista)):
    if len(lista)==i:
        break
    x=sorted(lista[i])
    func01(x[0],x[1],x[2])
    if tflag==0:
        break
    i+=1
print(t,ch,e,d)
A, B = map(int, input().split())
res = A * B
print(int(res))

### FIXED CODE ###

from decimal import Decimal, ROUND_FLOOR

A, B = input().split()
A = Decimal(A)
B = Decimal(B)
res = A * B
print(res.quantize(Decimal("1"), rounding=ROUND_FLOOR))
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()
    if a[-1] != 2:
        return -1
    b = 2
    c = 4
    for i in range(n-2,-1,-1):
        t = a[i]
        if b * 2 <= t:
            return -1
        if b % t == 0:
            pass
        elif b > t:
            b = (b+t) - (b%t)
        else:
            b = t
        if c % t == 0:
            pass
        elif c > t:
            c = (c+t) - (c%t)
        else:
            return -1
    bb = b
    cc = c-1
    for i in range(n):
        bb -= bb%a[i]
        cc -= cc%a[i]
    if bb != 2:
        return 1 / 0
    if cc != 2:
        while 1:
            cc += 1

    return '{} {}'.format(b, c-1)

print(main())




### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

# 下がtrue, falseになる最小値を返す
def bs(f, mi, ma):
    mm = -1
    while ma > mi:
        mm = (ma+mi) // 2
        if f(mm):
            mi = mm + 1
        else:
            ma = mm
    if f(mm):
        return mm + 1
    return mm

def main():
    n = I()
    a = LI()

    def f(i):
        for c in a:
            i -= i%c
        return i < 2

    def g(i):
        for c in a:
            i -= i%c
        return i <= 2

    mi = bs(f,0,sum(a)+3)
    ma = bs(g,0,sum(a)+3) - 1
    if mi <= ma:
        return '{} {}'.format(mi,ma)
    return -1




print(main())
N = int(input())
H = map(int, input().split())
cnt = 0
ans = []
prev = 0
for i,h in enumerate(H):
    if i==0:
        pass
    else:
        if prev>=h:
            cnt += 1
        else:
            ans.append(cnt)
            cnt = 0
    prev = h
print(max(ans))

### FIXED CODE ###

N = int(input())
H = list(map(int, input().split()))
ans = []
c = 0
for i in range(1, N):
    if H[i-1]>=H[i]:
        c += 1
    else:
        ans.append(c)
        c = 0
ans.append(c)
print(max(ans))
x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
c=0
for i in range(x[0]):
  if y[i]>=x[1]:
    c++
    continue
print(c)


### FIXED CODE ###

x = [int(x) for x in input().split()]
y = [int(x) for x in input().split()]
c=0
for i in range(x[0]):
  if y[i]>=x[1]:
    c+=1
    continue
print(c)
N,M= map(int, input().split())
a = list(map(int,input().split()))
b = sorted(a,reverse = True)
total = 0
for i in range(len(b)):
  total += b[i]

if b[M]<total/(4*M):
  print("No")
else:
  print("Yes")

### FIXED CODE ###

N,M= map(int, input().split())
a = list(map(int,input().split()))
b = sorted(a,reverse = True)
total = 0
for i in range(len(b)):
  total += b[i]

if b[M-1]<total/(4*M):
  print("No")
else:
  print("Yes")
S = input()
K = int(input())
if S[0] == "1" and K == 1:
    print(1)
    exit()
A = S.lstrip("1")
print(A[0])

### FIXED CODE ###

S = input()
K = int(input())
if S[:K].count("1") == K:
    print(1)
    exit()
A = S.lstrip("1")
print(A[0])
import numpy as np

N = int(input())
*A, = map(int, input().split())
A = np.array(A, dtype='int64')
C = np.diag([1 for _ in range(N)]) + np.diag([1 for _ in range(N-1)], k=1)
C[-1, 0] = 1
X = np.linalg.solve(C, A)
X = list(map(int, X))
print(*X)

### FIXED CODE ###

N = int(input())
*A, = map(int, input().split())
X = [0]*N
X_total = sum(A)
A_odd = sum([A[i % N] for i in range(1, N, 2)])
A_even = sum([A[i % N] for i in range(2, N+1, 2)])
for i in range(N):
    X[i] = X_total - 2*(A_odd if i % 2 == 0 else A_even)
    if i % 2 == 0: A_odd += A[i] - A[(i+1) % N]
    else: A_even += A[i] - A[(i+1) % N]
print(*X)
N,M=[int(x) for x in input().split()]
I=[[] for i in range(N)]
E=[]
V=[0 for i in range(N)]

def visit(n):
    total=0
    if (n > -1 and V[n]==0):
        V[n]=1
        total=1
        for i in I[n]:
            total+=visit(i)
    return total

for i in range(M):
    A,B=[int(x) for x in input().split()]
    A,B=A-1,B-1
    I[A].append(B)
    I[B].append(A)
    E.append((A,B))

while len(E)>0:
    A,B=E.pop(0)
    I[A].pop(I[A].index(B))
    I[B].pop(I[B].index(A))
    V=[0 for i in range(N)]

    Z=N
    i=0
    for n in range(N):
        total=visit(n)
        Z=Z-total
        i=i+(total*Z)
    print(i)



### FIXED CODE ###

N,M=[int(x) for x in input().split()]
G=[-1] * N
W=[0] * N
E=[]

result=[]
ans = int(N * (N-1) / 2)

def union(i,j):
    gi=find(i)
    gj=find(j)

    if(gi!=gj):
        if W[gi] > W[gj]:
            G[gi]+=G[gj]
            G[gj]=gi
        else:
            G[gj]+=G[gi]
            G[gi]=gj
            if W[gi]==W[gj]:
                W[gj]+=1

def find(i):
    if G[i]<0:
        return i;
    else:
        G[i]=find(G[i])
        return G[i]

for m in range(M):
    A,B=[int(x) for x in input().split()]
    E.append((A-1,B-1))

for m in range(M):
    result.append(ans)
    if(ans>0):
        A,B=E.pop()
        ga,gb=find(A),find(B)
        if (ga!=gb):
            ans=ans-(G[ga]*G[gb])
            union(A,B)

for r in reversed(result):
    print(r)
while True:
	a=0
	num=list(map(int,input().split()))
	if num[0]==0 and n[1]==0:
		break
	else:
		for i in range(1,num[0]+1):
			for j in range(i+1,num[0]+1):
				for k in range(j+1,num[0]+1):
					if i+j+k==num[1]:
						a+=1
						print(i,j,k)
					
			


### FIXED CODE ###

while True:
	a=0
	num=list(map(int,input().split()))
	if num[0]==0 and num[1]==0:
		break
	else:
		for i in range(1,num[0]+1):
			for j in range(i+1,num[0]+1):
				for k in range(j+1,num[0]+1):
					if i+j+k==num[1]:
						a+=1
		print(a)
def insertion_sort(array, g, result):
    for i in range(g, len(array)):
        v = array[i]
        j = i - g
        while j >= 0 and array[j] > v:
            array[j + g] = array[j]
            j -= g
            result['count'] += 1
        A[j + g] = v


def g_gene(n):
    yield 1
    for i in range(2, n):
        x = int((3 ** i - 1) / 2)
        if x < n:
            yield x
        else:
            raise StopIteration()


def shell_sort(array, result):
    result['count'] = 0
    G = list(reversed(list(g_gene(len(array)))))
    m = len(G)
    result['G'] = G
    result['m'] = m
    for i in range(m):
        insertion_sort(array, G[i], result)


if __name__ == '__main__':
    import sys
    n = int(sys.stdin.readline())
    A = []
    for _ in range(n):
        A.append(int(sys.stdin.readline()))
    result = {}
    shell_sort(A, result)
    print(result['m'])
    print(' '.join(map(str, result['G'])))
    print(result['count'])
    for a in A:
        print(a)

### FIXED CODE ###

def insertion_sort(array, g, result):
    for i in range(g, len(array)):
        v = array[i]
        j = i - g
        while j >= 0 and array[j] > v:
            array[j + g] = array[j]
            j -= g
            result['count'] += 1
        A[j + g] = v


def g_gene(n):
    yield 1
    for i in range(2, n):
        x = int((3 ** i - 1) / 2)
        if x < n:
            yield x
        else:
            raise StopIteration()


def shell_sort(array, result):
    result['count'] = 0
    G = list(reversed(list(g_gene(len(array)))))
    m = len(G)
    result['G'] = G
    result['m'] = m
    for i in range(m):
        insertion_sort(array, G[i], result)


if __name__ == '__main__':
    import sys
    n = int(sys.stdin.readline())
    A = []
    for _ in range(n):
        A.append(int(sys.stdin.readline()))
    result = {}
    shell_sort(A, result)
    print(result['m'])
    print(' '.join(map(str, result['G'])))
    print(result['count'])
    for a in A:
        print(a)
n,m,q=map(int,input().split(" "))
arr=[list(map(int, input().split(" "))) for i in range(q)]
ans=0
for i1 in range(1,m+1):
	for i2 in range(i1,m+1):
		for i3 in range(i2, m+1):
			for i4 in range(i3, m+1):
				for i5 in range(i4, m+1):
					for i6 in range(i5, m+1):
						for i7 in range(i6, m+1):
							for i8 in range(i7, m+1):
								for i9 in range(i8, m+1):
									for i10 in range(i9, m+1):
										tmp=0
										arrs=[i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
										for a,b,c,d in arr:
											if arrs[b]-arrs[a]==c:
												tmp+=d
										ans=max(ans,tmp)
print(ans)

### FIXED CODE ###

n,m,q=map(int,input().split(" "))
arr=[list(map(int, input().split(" "))) for i in range(q)]
ans=0
for i1 in range(1,m+1):
	for i2 in range(i1,m+1):
		for i3 in range(i2, m+1):
			for i4 in range(i3, m+1):
				for i5 in range(i4, m+1):
					for i6 in range(i5, m+1):
						for i7 in range(i6, m+1):
							for i8 in range(i7, m+1):
								for i9 in range(i8, m+1):
									for i10 in range(i9, m+1):
										tmp=0
										arrs=[0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10]
										for a,b,c,d in arr:
											if arrs[b]-arrs[a]==c:
												tmp+=d
										ans=max(ans,tmp)
print(ans)
N=int(input())
A=list(map(int,input().split()))
A.sort()
max_a=A[-1]
dp=[True]*(max_a+1)
ans=0
for i in range(N):
  a=A[i]
  #print(a)
  if dp[a]==True: 
    if i==N-1:
      ans+=1
    elif a!=A[i+1]:
      ans+=1   
    k=a
    while k<=max_a:
      dp[k]=False    
      k+=a
print(ans)

### FIXED CODE ###

X=list(map(int,input().split()))
for i in range(5):
  if X[i]!=i+1:
    print(i+1);exit()
W, H, x, y, r = map(int, raw_input().split))
 
if r <= x and x+r <= W and r <= y and y+r <= H:
 print 'Yes'
else:
 print 'No'

### FIXED CODE ###

W, H, x, y, r = map(int, raw_input().split())
 
if r <= x and x + r <= W and r <= y and y + r <= H:
 print 'Yes'
else:
 print 'No'
N=int(input())
 
A=list(map(int,input().split()))
#B=[]
#C=[]
D=[0]*1000000
E=[0]*1000000
 
ans=0
 
for i in range(N):
  #B.append(A[i]+i+1)
  #C.append(-A[i]+i+1)
  D[A[i]+i]+=1
  E[-A[i]+i]+=1
 
for i in range(len(D)):
  #ans+=C.count(i)
  ans+=D[i]*E[i]
 
print(ans)

### FIXED CODE ###

N=int(input())
 
A=list(map(int,input().split()))
#B=[]
#C=[]
D=[0]*1000000
E=[0]*1000000
 
ans=0
 
for i in range(N):
  if A[i]<500000:
  #B.append(A[i]+i+1)
  #C.append(-A[i]+i+1)
  #if -A[i]+i>=0:
  	D[A[i]+i]+=1
  	E[-A[i]+i]+=1
   #print(E[-A[i]+i],-A[i]+i)

for i in range(1000000):
  #ans+=C.count(i)
  ans+=D[i]*E[i]
 
print(ans)
s = input()
print(s.count(1))


### FIXED CODE ###

s = input()
print(s.count("1"))
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N,M = inputIL()
A = inputIL()

R = [A[0]%M]

for i in range(1,N):
  R.append((R[-1]+A[i])%M)
  
  
key = set(R)
dic = {i:0 for i in key}
for item in R:
  dic[item]+=1


#print(dic)
ans = dic[0]*(dic[0]+1)/2
for i in key:
  if i !=0 :
    ans += dic[i]*(dic[i]-1)/2

  
print(int(ans))

### FIXED CODE ###

N,M = map(int,input().split())
A = list(map(int,input().split()))

R = [A[0]%M]


for i in range(1,N):
  R.append((R[-1]+A[i])%M)



import collections
c = collections.Counter(R)


ans = 0
for key in c:
  if key == 0:
    ans += (c[key]*(c[key]+1))/2
  else:
    ans += (c[key]*(c[key]-1))/2
  



print(int(ans))
print("ABC" if int(input() < 1200 else "ARC")

### FIXED CODE ###

print(['ARC','ABC'][int(input())<1200])
import math

n = int(input())
a = list(map(int, input().split( )))
ans = 0
for i in range(n):
    ans = math.gcd(ans,a[i])
print(ans)

### FIXED CODE ###

import fractions

n = int(input())
a = list(map(int, input().split( )))
ans = 0
for i in range(n):
    ans = fractions.gcd(ans,a[i])
print(ans)
H_1,M_1,H_2,M_2,K = map(int,input().split())
MaxH = H_2 - H_1
if M_1 > M_2:
    MaxM = M_2 - M_1
if M_1 < M_2:
    MaxM = M_1 - M_2
useTime = MaxH*60 + MaxM
useTime -= K
print(useTime)

### FIXED CODE ###

H_1,M_1,H_2,M_2,K = map(int,input().split())
if H_2 < H_1:
    H_2 += 24
MaxH = abs(H_2 - H_1)
if M_1 >= M_2:
    MaxM = M_2 - M_1
if M_1 < M_2:
    MaxM = abs(M_1 - M_2)
useTime = MaxH*60 + MaxM
useTime -= K
print(useTime)
import sys
N = int(input())
for i4 in range(N):
  for i7 in range(N):
    if i4 * 4 + i7 * 7 == N:
      print("Yes")
      sys.exit(0)
print("No")

### FIXED CODE ###

N, K=map(int, input().split())
print(0 if N%K==0 else 1);
a,b=map(int,input().split())
y=[]
for i in range(a-1):
    v=list(map(int,input().split()))
    y.append(v)
y.sort(key=lambda net:net[0])
ans=[0]*a
for j in range(b):
    c,d=map(int,input().split())
    ans[c-1]=ans[c-1]+d
for k in range(a-1):
    ans[zz]=ans[y[k][1]-1]+ans[y[k][0]-1]
print(*ans,sep=" ")


### FIXED CODE ###

a,b=map(int,input().split())
y=[]
for i in range(a-1):
    v=list(map(int,input().split()))
    y.append(v)
y.sort(key=lambda n:n[0])
ans=[0]*a
for j in range(b):
    c,d=map(int,input().split())
    ans[c-1]=ans[c-1]+d
for k in range(a-1):
    ans[y[k][1]-1]=ans[y[k][1]-1]+ans[y[k][0]-1]
print(*ans,sep=" ")
L, R = map(int, input().split())


if R-L>=L:
    print(0)
else:
    for l in range(L,R):
        for r in range(l+1, R+1):
            z = min(z, ((l % 2019) * (r % 2019)) % 2019)
    print(z)


### FIXED CODE ###

L, R = map(int, input().split())

z = 2019

if R-L>= 2019:
    print(0)
else:
    for l in range(L,R):
        for r in range(l+1, R+1):
            z = min(z, ((l % 2019) * (r % 2019)) % 2019)
    print(z)
# coding: utf-8

    count += 1

### FIXED CODE ###

# coding: utf-8
# Here your code !
count = 1

while 1:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    data =[]
    dp = [[[-1,0]for i in range(W+1)]for i in range(N+1)]
    for i in range(N):
        data.append(input().split(","))

    for i in range(W+1):
        dp[N][i][0] = 0
    for i in reversed(range(N)):
        for capa in range(W+1):
            if int(data[i][1]) > capa:
                dp[i][capa][0] = dp[i+1][capa][0]
                dp[i][capa][1] = dp[i+1][capa][1]
            else:
                dp[i][capa][0] = max(int(data[i][0])+dp[i+1][capa-int(data[i][1])][0],dp[i+1][capa][0])
                dp[i][capa][1] = dp[i+1][capa][1]
                if dp[i][capa][0] != dp[i+1][capa][0]:
                    dp[i][capa][1] = int(data[i][1])+dp[i+1][capa-int(data[i][1])][1]
    print("Case " + str(count) +":")
    print(max(max(dp))[0])
    print(max(max(dp))[1])
 
    count += 1
# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:30 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    fixed = set([])
    while True:
        # 最短から貪欲に伸ばして行く
        x, y = H, W  # init
        for i in range(1, H+1):
            for j in range(1, W+1):
                if s[i][j] != -1 and s[i][j] <= s[x][y] and "{}_{}".format(i, j) not in fixed:
                    x, y = i, j
                    
        fixed.add("{}_{}".format(x, y))
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1:
            s[x][y-1] = min(s[x][y]+1, s[x][y-1])
            # fixed.add("{}_{}".format(x, y-1))
        if s[x][y+1] != -1:
            s[x][y+1] = min(s[x][y]+1, s[x][y+1])
            # fixed.add("{}_{}".format(x, y+1))
        if s[x-1][y] != -1:
            s[x-1][y] = min(s[x][y]+1, s[x-1][y])
            # fixed.add("{}_{}".format(x-1, y))
        if s[x+1][y] != -1:
            s[x+1][y] = min(s[x][y]+1, s[x+1][y])
            # fixed.add("{}_{}".format(x+1, y))
    
    return n_white - (shortest + 1) if shortest < H*W else


print(main())


### FIXED CODE ###

# coding: utf-8

# https://atcoder.jp/contests/abc088/tasks/abc088_d
# 19:01-20:04 chudan
# 20:17-20:53 done


def main():
    H, W = map(int, input().split())

    INF = 10**10
    n_white = 0
    s = [[-1] * (W+2) for _ in range(H+2)]
    for i in range(H):
        for j, x in enumerate(input()):
            # white => 1, black => -1
            s[i+1][j+1] = INF if x == "." else -1
            n_white += 1 if x == "." else 0
    s[1][1] = 0

    queue = [(1, 1)]
    queue_size = 1
    queue_idx = 0
    while True:
        # 最短から貪欲に伸ばして行く
        if queue_idx == queue_size:
            # cannot arrive
            return -1
        
        x, y = queue[queue_idx]
        queue_idx += 1
        
        if x == H and y == W:
            shortest = s[x][y]
            break

        if s[x][y-1] != -1 and s[x][y-1] > s[x][y]+1:
            s[x][y-1] = s[x][y]+1
            queue.append((x, y-1))
            queue_size += 1
        if s[x][y+1] != -1 and s[x][y+1] > s[x][y]+1:
            s[x][y+1] = s[x][y]+1
            queue.append((x, y+1))
            queue_size += 1
        if s[x-1][y] != -1 and s[x-1][y] > s[x][y]+1:
            s[x-1][y] = s[x][y]+1
            queue.append((x-1, y))
            queue_size += 1
        if s[x+1][y] != -1 and s[x+1][y] > s[x][y]+1:
            s[x+1][y] = s[x][y]+1
            queue.append((x+1, y))
            queue_size += 1
    
    return n_white - (shortest + 1)


print(main())