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