import sys N=int(input()) K=int(input()) N=N%6 if N==0: print(4) sys.exit() K=K%2 c=(N**K)%7 L=[2,8,5,7,1,4] print(L[c-1])