N = list(map(int, list(input()))) M = list(map(int, list(input()))) n = N[-1] if len(M) >= 3: m = M[-1] % 4 + 100 elif len(M) == 2: m = M[0] * 10 + M[1] else: m = M[0] print(pow(n, m, 10))