import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.set_int_max_str_digits(10**6) N = int(input()) K = int(input()) A = [4,2,8,5,7,1] answer = A[pow(N,K,6)] print(answer)