N = input() M = input() n = int(N[-1]) m = int(M[-2:]) % 4 print(pow(n, m, 10))