InputData = list()
PrimeCache = list()

def PrimeInitialize(Number):
    global PrimeCache
    Numbers = [True] * Number
    Numbers[0], Numbers[1] = False, False
    Pointer = 2
    PrimeCache = []
    while Pointer < len(Numbers):
        if Numbers[Pointer]:
            PrimeCache.append(Pointer)
            for Cross in range(Pointer * 2, len(Numbers), Pointer):
                Numbers[Cross] = False
        Pointer += 1

def PrimeDecomposite(Number):
    global PrimeCache
    
    if Number == 0 or Number == 1: return 0
    Prime,PrimeCount = 2, 0
    for Prime in PrimeCache:
        if not Number % Prime: PrimeCount += 1
        while not Number % Prime:
            Number = Number // Prime
        if Number == 1: return PrimeCount
    
    while Prime <= Number:
        if not Number % Prime: PrimeCount += 1
        while not Number % Prime:
            Number = Number // Prime
        Prime += 1
        if Number == 1: return PrimeCount


for _ in range(5): InputData.append(list(map(int, input().split(' '))))
PrimeInitialize(1000000)
for _ in range(5):
    Number, TargetIndex = str(InputData[_][0]), len(str(InputData[_][0]))-InputData[_][1]
    Result, Pointer = str(), 0
    while Pointer < len(Number):
        if Pointer < TargetIndex:
            ReplaceNum = str(int(Number[Pointer]) + int(Number[TargetIndex]))
        elif Pointer == TargetIndex:
            ReplaceNum = str(PrimeDecomposite(int(Number)))
        else:
            ReplaceNum = str(abs(int(Number[Pointer]) - int(Number[TargetIndex])))
        
        Result += ReplaceNum
        Pointer += 1
    print(Result)