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()))) dp = [0 for _ in range(len(M))] dp[0] = M[0]%T for i in range(1,len(M)): dp[i] = (dp[i-1]*10+M[i])%T ans = n for i in range(1,dp[len(M)-1]): ans = (ans*n)%10 print(ans)