n = int(input()) * 10 ** 100 m = int(input()) from math import gcd d = gcd(n, m) n //= d m //= d if m != 1: ans = -1 else: while n % 10 == 0: n //= 10 ans = n % 10 print(ans)