N=int(input()) K=int(input()) K1=K if(K>3): K1= K % 2 if(N>6): N= N % 6 ans=(N**K1) % 6 list=[2,8,5,7,1,4] print(list[ans-1])