N, M = int(input()), int(input()) def to_and_five(num, lists={2:0, 5:0}): if num == 1: return lists if num % 2 == 0: lists[2] += 1 return to_and_five(num // 2, lists) if num % 5 == 0: lists[5] += 1 return to_and_five(num // 5, lists) return False def prime(n, m): if n < m: n, m = m, n if not (tmp := n % m): return m else: return prime(m, tmp) if N % M == 0: ans = N // M while not ans % 10: ans //= 10 ans %= 10 else: N, M = N // (tmp := prime(N, M)), M // tmp if (tmp := to_and_five(M)): if N >= 10: N %= 10 if tmp[2] > tmp[5]: for i in range(tmp[2] - tmp[5]): N *= 5 elif tmp[2] < tmp[5]: for i in range(tmp[5] - tmp[2]): N *= 2 ans = N % 10 else: ans = -1 print(ans)