def solve(): N=int(input()) K=int(input()) N%=6 return "428571"[pow(N,K,6)] #================================================== print(solve())