N=int(input()) K=int(input()) res = '428571' print(res[pow(N,K,6)])