n = int(input()) k = int(input()) n %= 6 x = 1 y = n assert(n != 0) for i in range(1, 6): if (n ** i) % 6 == 1: k %= i break if i == 1: continue; if (n ** i) % 6 == n: k %= i - 1 break while k != 0: if k % 2 == 1: x = x * y % 6 y = y * y % 6 k = k // 2 n = (n + 6 - 1) % 6 s = [2, 8, 5, 7, 1, 4] print(s[n])