N = input() M = input() if M == "0": print(1) else: M = (int(M[-3:]) - 1) % 4 + 1 N = int(N[-1]) print(N**M%10)