N = int(input()) K = int(input()) s = "428571" t = pow(N%6,K,6) print(s[t])