L = [4, 2, 8, 5, 7, 1] n = str(input()) k = int(input()) n = list(n) n.reverse() N = 10**5+50 P = [0]*N P[0] = 1 for i in range(1, N): P[i] = P[i-1]*10 P[i] %= 6 x = 0 for i, c in enumerate(n): x += int(c)*P[i] x %= 6 x = pow(x, k, 6) print(L[x])