N = list(map(int, list(input()))) M = list(map(int, list(input()))) m = M[-1] n = N[-1] if m == 0: print(0) exit() m -= 1 m %= 4 m += 1 print(pow(n, m, 10))