N = input().strip() M = input().strip() n = int(N[-1]) k = 1 x = (n**2)%10 k = 2 while x!=n: k += 1 x = (x*n)%10 T = k-1 if M=="0" or N=="1": print(1) else: m = int(M)%T if m==0: m = T k = 1 x = n while k