n1=input()
n2=input()
n3=input()
n4=input()
n5=input()
n12345=[n1,n2,n3,n4,n5]
def result(n,p):
    newn=str(n)
    pindex=len(newn)-p
    nList=[]
    pth=int(newn[len(newn)-p])
    for i in range(len(newn)):
        nList.append(newn[i])
    
    for i in range(pindex):
        nList[i]=str(int(newn[i])+pth)

    for i in range(len(newn)-pindex-1):
        nList[i+pindex+1]=str(abs(int(newn[i+pindex+1])-pth))

    nList[pindex]=str(getPF(n))
    result=''
    for i in nList:
        result+=i
    return result
def getPF(n):
    maxNum=int(n**0.5)
    count=0
    divisor=2
    while divisor<=maxNum and n>1:
        if n%divisor==0:
            while n%divisor==0: n//=divisor
            count+=1
        divisor+=1
    if n>1: count+=1
    return count
for i in n12345:
    n,p=i.split()
    print(result(int(n),int(p)))