#!/usr/bin/python3 from collections import defaultdict, deque from fractions import gcd from functools import reduce from operator import mul from random import randrange def rabin_miller(n): if n < 2: return False if n != 2 and n % 2 == 0: return False s = n - 1 while s % 2 == 0: s >>= 1 for _ in range(10): a = randrange(n-1) + 1 tmp = s mod = pow(a, tmp, n) while tmp != n-1 and mod != 1 and mod != n-1: mod = (mod * mod) % n tmp *= 2 if mod != n-1 and tmp % 2 == 0: return False return True def brent(n): if n % 2 == 0: return 2 x = randrange(0, n) c = randrange(1, n) m = randrange(1, n) y, r, q = x, 1, 1 g, ys = 0, 0 while g <= 1: x = y for _ in range(r): y = (y*y+c)%n k = 0 while k < r and g <= 1: ys = y for _ in range(min(m, r-k)): y = (y*y+c)%n q = (q*abs(x-y)) % n g, k = gcd(q,n), k+m r <<= 1 if g == n: while g <= 1: ys = (ys*ys+c)%n g = gcd(abs(x-ys), n) return g def factorize(n): que = deque() res = defaultdict(int) if n == 1: res[1] = 1 return res que.append(n) while que: l = que.pop() if rabin_miller(l): res[l] += 1 continue d = brent(l) que.append(d) if d != l: que.append(l // d) return res ### n, m = int(input()), int(input()) x = factorize(n) y = factorize(m) yy = max(y[2], y[5]) x[2] += yy - y[2] x[5] += yy - y[5] y[2] = 0 y[5] = 0 xx = min(x[2], x[5]) x[2] -= xx x[5] -= xx for k in y.keys(): if y[k] > x[k]: print(-1) exit(0) else: x[k] -= y[k] res = reduce(mul, (k**v for k, v in x.items())) % 10 print(res)