def gcd(A,B): if A>B: return gcd(B,A) elif B%A==0: return A else: return gcd(B%A,A) N=int(raw_input()) M=int(raw_input()) g = gcd(N,M) N/=g M/=g tM=M while N%10 ==0: N/=10 while tM%10 == 0: tM/=10 if tM==1: print N%10 exit() while tM % 2 == 0: tM/=2 if tM==1: print 5 exit() table = [(i*2)%10 for i in range(10)] key = N%10 while tM %5 == 0: tM/=5 key = table[key] if tM==1: print key exit() print -1