N, M = int(input()), int(input()) if M == 0: print(1) else: print((N ** (M % 4 + 4)) % 10)