n = input() n_6 = sum(map(int, n)) % 3 * 4 + int(n[-1]) % 2 * 3 + 6 k = int(input()[-1]) % 2 + 2 print("428571"[n_6 ** k % 6])