n=int(input())%6;k=int(input())%2 d={1:2,2:8,3:5,4:7,5:1,0:4} if (n==2 or n==5) and k==0: print(d[6-n]) else: print(d[n])