N=int(input()) K=int(input()) N=N%6 if N==0: print(4) elif N==1: print(2) elif N==2: if K%2==1: print(8) else: print(7) elif N==3: print(5) elif N==4: print(7) elif N==5: if K%2==1: print(1) else: print(2)