N = int(input()) K = int(input()) num = [2,8,5,7,1,4,2] print(num[(pow(N,K,6)-1)%6])