N = input().strip() n = int(N[-1]) cur = n T = 1 while True: ncur = (cur*n)%10 if ncur==n:break T += 1 cur = ncur M = list(map(int,list(input()))) if M[0]==0: print(1) else: dp = [0 for _ in range(len(M))] dp[0] = M[0]%10 for i in range(1,len(M)): dp[i] = (dp[i-1]*10+M[i])%T ans = n if dp[-1]==0: dp[-1]=T for i in range(1,dp[len(M)-1]): ans = (ans*n)%10 print(ans)