N = int(input()[-1]) M = int(input()) kk = 1 for m in range(M): kk *= N if kk == 0: break k = (abs(kk-1))%6 print('285714'[k])