N = int(input()) M = int(input()) a = N%10 n = [a] for i in range(5): a *= (N%10) a %= 10 n.append(a) if a==(N%10): break print(n[M%(i+1)-1])