inp = open("mcd.in", "r")
outp = open("mcd.out", "w")

a, b = map(int, inp.readline().split())

def gcd(a, b):
    if not b:
        return a
    return gcd(b, a % b)

def calc(num):
    return sum(list(map(int, list(str(num)))))

d = gcd(a, b)

c = 1

ans = d
best = calc(d)

while (c * c <= d):
    if (d % c == 0):
        num = c
        tmp = calc(num)
        if (tmp > best or (tmp == best and ans < num)):
            best = tmp
            ans = num

        num = d // c
        tmp = calc(num)
        if (tmp > best or (tmp == best and ans < num)):
            best = tmp
            ans = num
    c += 1

outp.write(str(ans) + "\n")