N = int(input()) K = input() mod6 = N%6 if mod6 == 2: if int(K[-1])%2==0: mod6 = 4 elif mod6 == 5: if int(K[-1])%2==0: mod6 = 1 S = '285714' print(S[(mod6-1)%6])