前言:祝大家新年快乐!

思路:暴力枚举。注意到题目的a,b数字最长为10,因此可以枚举a和b的所有子序列,然后判断是否满足条件,时间复杂度为,为大概是量级左右,可以通过

具体来说,我们可以枚举保留的位数个数i和j,然后用组合数函数combinations(a, x)表示从a这个迭代器或者数组等这种存放元素的结构中,选出x个数进行组合。接着,我们就可以生成保留相应位数的数字na和nb了,然后判断一下是否满足“na % nb == 0 or nb % na == 0”的条件,就可以收集答案了(注意是收集删除次数)。最终,枚举完成后输出答案即可

注意:本题至少需要保留一个数位,不能变成空串,因为空串不能算成0

代码:

import sys
from itertools import combinations
input = lambda: sys.stdin.readline().strip()

import math
inf = 10 ** 18

def I():
    return input()

def II():
    return int(input())

def MII():
    return map(int, input().split())

def LI():
    return input().split()

def LII():
    return list(map(int, input().split()))

def LFI():
    return list(map(float, input().split()))

fmax = lambda x, y: x if x > y else y
fmin = lambda x, y: x if x < y else y
isqrt = lambda x: int(math.sqrt(x))

'''

'''

def solve():
    a, b = MII()
    sa, sb = str(a), str(b)
    m, n = len(sa), len(sb)

    ans = inf

    for i in range(1, m + 1):
        for pos_a in combinations(range(m), i):
            na = int(''.join(sa[p] for p in pos_a))

            for j in range(1, n + 1):
                for pos_b in combinations(range(n), j):
                    nb = int(''.join(sb[p] for p in pos_b))

                    if na % nb == 0 or nb % na == 0:
                        res = (m - i) + (n - j)
                        ans = fmin(ans, res)

    print(ans if ans < inf else -1)

t = 1
# t = II()
for _ in range(t):
    solve()