import sys sys.setrecursionlimit(10 ** 6) import numpy as np def II(): return int(sys.stdin.readline()) def main(): def lowest(a): while a%10==0:a//=10 a%=10 print(a) exit() def cnt_factor(m): c2=c5=0 while m&1==0: m>>=1 c2+=1 while m%5==0: m//=5 c5+=1 if m==1:return c2,c5 return -1,-1 n=II() m=II() g=np.gcd(m,n) n,m=n//g,m//g if m==1:lowest(n) c2,c5=cnt_factor(m) if c2==-1:print(-1) elif c2==c5:lowest(n) elif c2>c5:print(5) else: #/5=*0.2なのでc5回2をかけたときの最下位の数が答え c5-=c2 n%=5 print(pow(2,c5,10)*n%10) main()