N = int(input()[-1]) M = input() if M == '0': print(1) quit() if N == 0: print(0) quit() M = int(M[-2:]) print(N**(M % 4) % 10)