N = int(input()) M = int(input()) ans = (pow(N%10, M%4 if M%4 != 0 else 4))%10 print(ans)